Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 1452E - Two Editorials
- Consider some participant's segment [l;r] and one of the author's segment [i;i+k−1]. How does the length of intersection change when you move i from left to right? It first increases until the centers of both segments coincide (that's the easiest to notice on the segments of the same length) and then decreases. The increase is totally symmetrical to the decrease.
- With that idea you can conclude that the author's segment, whose center is the closest to the center of participant's segment, has the larger intersection length.
- Let's sort the participants' segments by their center. You can see that the first author will be optimal for the prefix of the segments and the second author — for the remaining suffix.
- So you can just iterate over the length of the prefix and update the answer with all options.
- Overall complexity: O(nlogn+nm).
- #include <bits/stdc++.h>
- #define forn(i, n) for (int i = 0; i < int(n); i++)
- using namespace std;
- struct seg{
- int l, r;
- };
- int main() {
- int n, m, k;
- cin >> n >> m >> k;
- vector<seg> a(m);
- forn(i, m){
- cin >> a[i].l >> a[i].r;
- --a[i].l;
- }
- sort(a.begin(), a.end(), [](const seg &a, const seg &b){
- return a.l + a.r < b.l + b.r;
- });
- vector<int> su(m + 1);
- forn(i, n - k + 1){
- int cur = 0;
- for (int j = m - 1; j >= 0; --j){
- cur += max(0, min(i + k, a[j].r) - max(i, a[j].l));
- su[j] = max(su[j], cur);
- }
- }
- int ans = su[0];
- forn(i, n - k + 1){
- int cur = 0;
- forn(j, m){
- cur += max(0, min(i + k, a[j].r) - max(i, a[j].l));
- ans = max(ans, cur + su[j + 1]);
- }
- }
- cout << ans << endl;
- return 0;
- }
- #include <bits/stdc++.h>
- using namespace std;
- const int N = 2000;
- array<int, 2> segment[N];
- int prefix[N];
- int value[N];
- int main() {
- ios_base::sync_with_stdio(false);
- cin.tie(nullptr);
- int n, m, k;
- cin >> n >> m >> k;
- for (int i = 0; i < m; ++i) {
- for (auto &j : segment[i]) {
- cin >> j;
- }
- --segment[i][0];
- }
- sort(segment, segment + m, [](array<int, 2> a, array<int, 2> b) {
- return a[0] + a[1] < b[0] + b[1];
- });
- for (int i = 0; i < m; ++i) {
- auto [l, r] = segment[i];
- for (int j = 0; j < n; ++j) {
- value[j] += max(0, min(j + k, r) - max(j, l));
- prefix[i] = max(prefix[i], value[j]);
- }
- }
- int ans = prefix[m - 1];
- fill(value, value + n, 0);
- for (int i = m - 1; i > 0; --i) {
- auto [l, r] = segment[i];
- for (int j = 0; j < n; ++j) {
- value[j] += max(0, min(j + k, r) - max(j, l));
- ans = max(ans, prefix[i - 1] + value[j]);
- }
- }
- cout << ans << "\n";
- }
- #include <bits/stdc++.h>
- int main() {
- using namespace std;
- ios_base::sync_with_stdio(false), cin.tie(nullptr);
- int N, M, K; cin >> N >> M >> K;
- vector<array<int, 2>> ranges(M);
- for (auto& a : ranges) { cin >> a[0] >> a[1]; a[0]--; }
- sort(ranges.begin(), ranges.end(), [&](const auto& a, const auto& b) { return a[0] + a[1] < b[0] + b[1]; });
- auto get_best = [&](const vector<int>& a) {
- int cur = 0;
- for (int i = 0; i < K; i++) {
- cur += a[i];
- }
- int ans = cur;
- for (int i = K; i < N; i++) {
- cur += a[i], cur -= a[i-K];
- ans = max(ans, cur);
- }
- return ans;
- };
- vector<int> lo_cnt(N);
- vector<int> hi_cnt(N);
- for (auto [x, y] : ranges) {
- for (int i = x; i < y; i++) hi_cnt[i]++;
- }
- int ans = get_best(lo_cnt) + get_best(hi_cnt);
- for (auto [x, y] : ranges) {
- for (int i = x; i < y; i++) hi_cnt[i]--, lo_cnt[i]++;
- ans = max(ans, get_best(lo_cnt) + get_best(hi_cnt));
- }
- cout << ans << '\n';
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement