structRange { int l, r; booloperator<(const Range & w) const { return r < w.r; } }range[N];
intmain(){ scanf("%d", &n); for (int i = 0; i < n; i++) { int a,b; scanf("%d%d", &a, &b); range[i] = {a, b}; } sort(range, range+n); int res = 0, ed = -2e9; // 判断为新的区间时,更新区间范围 for (int i = 0; i < n; i++) if (ed < range[i].l) { res++; ed = range[i].r; } printf("%d\n", res); return0; }
structRange{ int l, r; booloperator<(const Range &w) const { return r < w.r; } }range[N];
intmain(){ scanf("%d", &n); for (int i = 0; i < n; i ++ ) { int a, b; scanf("%d%d", &a, &b); range[i] = {a, b}; } sort(range, range+n); int res = 0, ed = -2e9; for (int i = 0; i < n; i ++ ) if (ed < range[i].l) { res++; ed = range[i].r; } printf("%d", res); return0; }
structRange{ int l, r; booloperator<(const Range & w) const { return l < w.l; } }range[N];
intmain(){ int st, ed; scanf("%d%d", &st, &ed); scanf("%d", &n); for (int i = 0; i < n; i ++ ) { int l, r; scanf("%d%d", &l, &r); range[i] = {l, r}; } sort(range, range + n); // 排序 int res = 0; bool success = false; for (int i = 0; i < n; i ++ ) { int j = i, r = -2e9; // 找出每一轮区间包含st的最大右端点 while (j < n && range[j].l <= st) { r = max(r, range[j].r); j++; } if (r < st) { res = -1; break; } res++; if (r >= ed) { success = true; break; } // i在for循环里,最后还要+1,所以如果i=j的话,相当于跳过了一个区间,所以只能i=j-1 i = j - 1; // 更新start至最大右端点 st = r; } if (!success) res = -1; printf("%d\n", res); return0; }
intmain(){ scanf("%d", &n); // 小根堆 priority_queue<int, vector<int>, greater<int>> heap; for (int i = 0; i < n; i ++ ) { int x; scanf("%d", &x); heap.push(x); } int res = 0; while(heap.size() > 1) { int a = heap.top(); heap.pop(); int b = heap.top(); heap.pop(); res += a + b; heap.push(a+b); } printf("%d\n", res); return0; }
intmain(){ scanf("%d", &n); for (int i = 0; i < n; i ++ ) scanf("%d", &t[i]); sort(t, t + n); // 1、排序 LL res = 0; for (int i = 0; i < n; i ++ ) res += t[i] * (n - i - 1); // 计算每一位 printf("%lld\n", res); return0; }
intmain(){ scanf("%d", &n); for (int i = 0; i < n; i ++ ) scanf("%d", &x[i]); sort(x, x + n); int res = 0; for (int i = 0; i < n; i ++ ) res += abs(x[i]-x[n/2]); printf("%d\n", res); return0; }
usingnamespacestd; typedefpair<int, int> PII; constint N = 5e4 + 10;
int n; vector<PII> v;
intmain(){ scanf("%d", &n); for (int i = 0; i < n; i ++ ) { int w, s; scanf("%d%d", &w, &s); v.push_back({w+s,w}); } sort(v.begin(), v.end()); int sum = 0, res = -2e9; for (int i = 0; i < n; i ++ ) { int w = v[i].second, s = v[i].first - w; res = max(res, sum - s); sum += w; } printf("%d\n", res); return0; }