constint N = 1e5 + 10; structRange { int l; int r; booloperator<(const Range &R) { return r < R.r; } }; structRange range[N]; intmain(){ int n; cin >> n; for (int i = 0; i < n; i++) { cin >> range[i].l >> range[i].r; } sort(range, range + n); int ed = -2e9; int res = 0; for (int i = 0; i < n; i++) { if (range[i].l > ed) { res++; ed = range[i].r; } } cout << res << endl; return0; }
#include<algorithm> #include<iostream> usingnamespace std; constint N = 1e5 + 10;
structRange { int l, r; booloperator<(const Range &W) { return l < W.l; } } range[N];
intmain(){ int n; int st, ed; cin >> st >> ed; cin >> n; for (int i = 0; i < n; i++) cin >> range[i].l >> range[i].r; sort(range, range + n); int res = 0; bool success = false; for (int i = 0; i < n; i++) { int j = i, r = -2e9; // 寻找所有能覆盖 start 点的区间中,右端点最靠右的区间 while (j < n && range[j].l <= st) { r = max(r, range[j].r); j++; } // 如果都不能覆盖住 start 点,无解 if (r < st) { res = -1; break; } res++; // 如果已经能够覆盖 end 点,成功 if (r >= ed) { success = true; break; } // 更新 start 点,从右端点最靠右的区间的右端点继续考虑 st = r; i = j - 1; } if (!success) res = -1; cout << res << endl; return0; }
intmain(){ int n; cin >> n; priority_queue<int, vector<int>, greater<int>> heap; while (n--) { int x; cin >> 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); } cout << res << endl; return0; }