CF_1452E - Two Editorials
作者:
nxdxml
,
2020-12-26 18:58:12
,
所有人可见
,
阅读 373
// 区间中点覆盖长度最大贪心
// 1452E - Two Editorials
#include<bits/stdc++.h>
using namespace std;
const int N = 2005;
int n, m, k, ans;
int suffix[N];
struct Interval
{
int l, r;
bool operator< (const Interval &Other) const {
return l + r < Other.l + Other.r; // 区间中点排序(处理重合)intersec相交
}
}Intervals[N];
int main(){
cin >> n >> m >> k;
for(int i = 0; i < m; i ++ ){
int l, r;
scanf("%d%d", &l, &r);
Intervals[i] = {l - 1, r}; // 左端点--这样右端点减去左端点就是长度
}
sort(Intervals, Intervals + m);
for(int i = 0; i + k <= n; i ++ ){
int sum = 0;
for(int j = m - 1; j >= 0; j -- ){
sum += max(0, min(i + k, Intervals[j].r) - max(i, Intervals[j].l));
suffix[j] = max(suffix[j], sum);
}
}
for(int i = 0; i + k <= n; i ++ ){
int sum = 0;
for(int j = 0; j <= m - 1; j ++ ){
sum += max(0, min(i + k, Intervals[j].r) - max(i, Intervals[j].l));
ans = max(ans, suffix[j + 1] + sum);
}
}
cout << ans;
return 0;
}