#include <bits/stdc++.h>
#pragma GCC optimize ("Ofast")
using namespace std;
const int MAXN = 2e3 + 25;
typedef long long ll;
int n, k; pair <ll, ll> a[MAXN];
vector <ll> dp[2][MAXN];
vector <ll> merge (vector <ll> &a, vector <ll> &b, ll c) {
vector <ll> ret;
int p1 = 0, p2 = 0;
while ((int)ret.size() < k && p1 < (int)a.size() && p2 < (int)b.size()) {
if (a[p1] < b[p2] + c) {
ret.push_back(a[p1++]);
} else {
ret.push_back(b[p2++] + c);
}
}
while ((int)ret.size() < k && p1 < (int)a.size()) ret.push_back(a[p1++]);
while ((int)ret.size() < k && p2 < (int)b.size()) ret.push_back(b[p2++] + c);
return ret;
}
int main () {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> k;
for (int i = 1; i <= n; i++) cin >> a[i].first >> a[i].second;
sort(a + 1, a + n + 1, [] (pair <ll, ll> &x, pair <ll, ll> &y) { return x.second < y.second; });
dp[0][0].push_back(0);
for (int i = 0, c = 0; i <= n - 1; i++, c ^= 1) {
for (int j = 0; j <= n; j++) dp[c ^ 1][j] = dp[c][j];
for (int j = a[i + 1].second - 1; j >= 0; j--) {
dp[c ^ 1][j + 1] = merge(dp[c ^ 1][j + 1], dp[c][j], a[i + 1].first);
}
/*
int sum = 0; int bad = -1;
for (int j = a[i + 1].second - 1; j >= 0; j--) {
sum += dp[c ^ 1][j + 1].size();
if (sum >= k) {
bad = j - 1; break;
}
}
for (int j = bad; j >= 0; j--) {
dp[c ^ 1][j].clear();
}*/
}
for (int i = n; i >= 0; i--) {
for (auto j : dp[n & 1][i]) {
cout << i << " " << j << '\n';
k--;
if (k == 0) break;
}
if (k == 0) break;
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
49 ms |
604 KB |
Output is correct |
2 |
Correct |
53 ms |
604 KB |
Output is correct |
3 |
Correct |
44 ms |
656 KB |
Output is correct |
4 |
Correct |
45 ms |
672 KB |
Output is correct |
5 |
Correct |
53 ms |
600 KB |
Output is correct |
6 |
Correct |
5 ms |
344 KB |
Output is correct |
7 |
Correct |
6 ms |
348 KB |
Output is correct |
8 |
Correct |
5 ms |
348 KB |
Output is correct |
9 |
Correct |
6 ms |
348 KB |
Output is correct |
10 |
Correct |
18 ms |
624 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
49 ms |
604 KB |
Output is correct |
2 |
Correct |
53 ms |
604 KB |
Output is correct |
3 |
Correct |
44 ms |
656 KB |
Output is correct |
4 |
Correct |
45 ms |
672 KB |
Output is correct |
5 |
Correct |
53 ms |
600 KB |
Output is correct |
6 |
Correct |
5 ms |
344 KB |
Output is correct |
7 |
Correct |
6 ms |
348 KB |
Output is correct |
8 |
Correct |
5 ms |
348 KB |
Output is correct |
9 |
Correct |
6 ms |
348 KB |
Output is correct |
10 |
Correct |
18 ms |
624 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
50 ms |
664 KB |
Output is correct |
15 |
Correct |
53 ms |
600 KB |
Output is correct |
16 |
Correct |
49 ms |
1104 KB |
Output is correct |
17 |
Correct |
46 ms |
604 KB |
Output is correct |
18 |
Correct |
52 ms |
668 KB |
Output is correct |
19 |
Correct |
5 ms |
604 KB |
Output is correct |
20 |
Correct |
6 ms |
604 KB |
Output is correct |
21 |
Correct |
6 ms |
604 KB |
Output is correct |
22 |
Correct |
6 ms |
608 KB |
Output is correct |
23 |
Correct |
18 ms |
604 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
352 KB |
Output is correct |
26 |
Correct |
0 ms |
352 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
100 ms |
896 KB |
Output is correct |
2 |
Correct |
94 ms |
608 KB |
Output is correct |
3 |
Correct |
75 ms |
608 KB |
Output is correct |
4 |
Correct |
77 ms |
604 KB |
Output is correct |
5 |
Correct |
89 ms |
652 KB |
Output is correct |
6 |
Correct |
5 ms |
564 KB |
Output is correct |
7 |
Correct |
6 ms |
600 KB |
Output is correct |
8 |
Correct |
5 ms |
604 KB |
Output is correct |
9 |
Correct |
7 ms |
604 KB |
Output is correct |
10 |
Correct |
27 ms |
600 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
600 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
860 KB |
Output is correct |
2 |
Correct |
1 ms |
860 KB |
Output is correct |
3 |
Correct |
2 ms |
856 KB |
Output is correct |
4 |
Correct |
1 ms |
1112 KB |
Output is correct |
5 |
Correct |
1 ms |
860 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
556 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
0 ms |
552 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
856 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
604 KB |
Output is correct |
2 |
Correct |
2 ms |
604 KB |
Output is correct |
3 |
Correct |
2 ms |
604 KB |
Output is correct |
4 |
Correct |
2 ms |
604 KB |
Output is correct |
5 |
Correct |
3 ms |
604 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
600 KB |
Output is correct |
11 |
Correct |
0 ms |
352 KB |
Output is correct |
12 |
Correct |
0 ms |
352 KB |
Output is correct |
13 |
Correct |
1 ms |
608 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
49 ms |
604 KB |
Output is correct |
2 |
Correct |
53 ms |
604 KB |
Output is correct |
3 |
Correct |
44 ms |
656 KB |
Output is correct |
4 |
Correct |
45 ms |
672 KB |
Output is correct |
5 |
Correct |
53 ms |
600 KB |
Output is correct |
6 |
Correct |
5 ms |
344 KB |
Output is correct |
7 |
Correct |
6 ms |
348 KB |
Output is correct |
8 |
Correct |
5 ms |
348 KB |
Output is correct |
9 |
Correct |
6 ms |
348 KB |
Output is correct |
10 |
Correct |
18 ms |
624 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
50 ms |
664 KB |
Output is correct |
15 |
Correct |
53 ms |
600 KB |
Output is correct |
16 |
Correct |
49 ms |
1104 KB |
Output is correct |
17 |
Correct |
46 ms |
604 KB |
Output is correct |
18 |
Correct |
52 ms |
668 KB |
Output is correct |
19 |
Correct |
5 ms |
604 KB |
Output is correct |
20 |
Correct |
6 ms |
604 KB |
Output is correct |
21 |
Correct |
6 ms |
604 KB |
Output is correct |
22 |
Correct |
6 ms |
608 KB |
Output is correct |
23 |
Correct |
18 ms |
604 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
352 KB |
Output is correct |
26 |
Correct |
0 ms |
352 KB |
Output is correct |
27 |
Correct |
100 ms |
896 KB |
Output is correct |
28 |
Correct |
94 ms |
608 KB |
Output is correct |
29 |
Correct |
75 ms |
608 KB |
Output is correct |
30 |
Correct |
77 ms |
604 KB |
Output is correct |
31 |
Correct |
89 ms |
652 KB |
Output is correct |
32 |
Correct |
5 ms |
564 KB |
Output is correct |
33 |
Correct |
6 ms |
600 KB |
Output is correct |
34 |
Correct |
5 ms |
604 KB |
Output is correct |
35 |
Correct |
7 ms |
604 KB |
Output is correct |
36 |
Correct |
27 ms |
600 KB |
Output is correct |
37 |
Correct |
0 ms |
348 KB |
Output is correct |
38 |
Correct |
0 ms |
348 KB |
Output is correct |
39 |
Correct |
0 ms |
600 KB |
Output is correct |
40 |
Correct |
1 ms |
860 KB |
Output is correct |
41 |
Correct |
1 ms |
860 KB |
Output is correct |
42 |
Correct |
2 ms |
856 KB |
Output is correct |
43 |
Correct |
1 ms |
1112 KB |
Output is correct |
44 |
Correct |
1 ms |
860 KB |
Output is correct |
45 |
Correct |
0 ms |
348 KB |
Output is correct |
46 |
Correct |
1 ms |
604 KB |
Output is correct |
47 |
Correct |
1 ms |
556 KB |
Output is correct |
48 |
Correct |
1 ms |
604 KB |
Output is correct |
49 |
Correct |
0 ms |
552 KB |
Output is correct |
50 |
Correct |
0 ms |
348 KB |
Output is correct |
51 |
Correct |
0 ms |
348 KB |
Output is correct |
52 |
Correct |
1 ms |
856 KB |
Output is correct |
53 |
Correct |
2 ms |
604 KB |
Output is correct |
54 |
Correct |
2 ms |
604 KB |
Output is correct |
55 |
Correct |
2 ms |
604 KB |
Output is correct |
56 |
Correct |
2 ms |
604 KB |
Output is correct |
57 |
Correct |
3 ms |
604 KB |
Output is correct |
58 |
Correct |
0 ms |
348 KB |
Output is correct |
59 |
Correct |
1 ms |
348 KB |
Output is correct |
60 |
Correct |
1 ms |
348 KB |
Output is correct |
61 |
Correct |
1 ms |
348 KB |
Output is correct |
62 |
Correct |
1 ms |
600 KB |
Output is correct |
63 |
Correct |
0 ms |
352 KB |
Output is correct |
64 |
Correct |
0 ms |
352 KB |
Output is correct |
65 |
Correct |
1 ms |
608 KB |
Output is correct |
66 |
Execution timed out |
5064 ms |
41140 KB |
Time limit exceeded |
67 |
Halted |
0 ms |
0 KB |
- |