#include <iostream>
#include <algorithm>
#include <utility>
#include <vector>
using namespace std;
using pp=pair<int,int>;
const int maxn = int(2e3) + 10;
#define pb push_back
int n, m, k;
pp d[maxn];
int tm[maxn*2], tn;
int user[maxn*2][2];
int F(int t) { return lower_bound(tm, tm+tn, t) - tm; }
bool vis[maxn];
int dp[2010][2010][2];
int Solve(int n, int k, vector<int> vv, vector<int> ve)
{
const int inf = int(1e9);
for (int j=0; j<=k; ++j) dp[0][j][0] = dp[0][j][1] = inf;
dp[0][0][0] = 0; dp[0][1][1] = vv[0];
for (int i=1; i<n; ++i) {
for (int j=0; j<=k; ++j) {
dp[i][j][0] = inf;
for (int t=0; t<2; ++t) {
dp[i][j][0] = min(dp[i][j][0], dp[i-1][j][t]);
}
dp[i][j][1] = inf;
if (j >= 1) {
if (dp[i-1][j-1][0] != inf) {
dp[i][j][1] = min(dp[i][j][1], dp[i-1][j-1][0] + vv[i]);
}
if (dp[i-1][j-1][1] != inf) {
dp[i][j][1] = min(dp[i][j][1], dp[i-1][j-1][1] + vv[i] - ve[i]);
}
}
}
}
return min(dp[n-1][k][0], dp[n-1][k][1]);
}
int main()
{
cin >> n >> m >> k;
for (int i=1; i<=n; ++i) {
int s, t; cin >> s >> t;
d[i] = {s, t};
tm[tn++] = s;
tm[tn++] = t;
}
tm[tn++] = 0;
tm[tn++] = m;
sort(tm, tm+tn);
sort(d+1, d+n+1);
for (int i=1; i<=n; ++i) {
auto [s, t] = d[i];
s = F(s); t = F(t)-1;
d[i] = {s, t};
user[s][0] = i;
user[t][1] = i;
}
vector<int> vv, ve;
for (int i=1; i<=n; ++i) if (!vis[i]) {
vis[i] = 1;
{ auto [s, t] = d[i];
if (s == t) {
vv.pb(tm[t+1]-tm[s]); ve.pb(0);
continue;
} }
{ auto [s, t] = d[i]; vv.pb(tm[t+1]-tm[t] + tm[s+1]-tm[s]); ve.pb(0); }
int ci = i;
while (true) {
auto [s, t] = d[ci];
int ni = user[t][0];
if (!ni) break;
vis[ni] = true;
auto [ns, nt] = d[ni];
vv.pb(tm[nt+1]-tm[nt] + tm[ns+1]-tm[ns]); ve.pb(tm[t+1]-tm[t]);
ci = ni;
}
}
int ans = m - Solve(n, n-k, vv, ve);
cout << ans << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
284 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
284 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
972 KB |
Output is correct |
14 |
Correct |
1 ms |
1228 KB |
Output is correct |
15 |
Correct |
1 ms |
1228 KB |
Output is correct |
16 |
Correct |
1 ms |
1228 KB |
Output is correct |
17 |
Correct |
1 ms |
1356 KB |
Output is correct |
18 |
Correct |
1 ms |
972 KB |
Output is correct |
19 |
Correct |
1 ms |
1192 KB |
Output is correct |
20 |
Correct |
1 ms |
1100 KB |
Output is correct |
21 |
Correct |
1 ms |
1356 KB |
Output is correct |
22 |
Correct |
1 ms |
1228 KB |
Output is correct |
23 |
Correct |
1 ms |
1228 KB |
Output is correct |
24 |
Correct |
1 ms |
1228 KB |
Output is correct |