#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);
}
if (i && a[i + 1].second == a[i].second) continue;
int sum = 0; int bad = -1;
for (int j = a[i + 1].second - 1; j >= 0; j--) {
sum += dp[c ^ 1][j].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 |
17 ms |
348 KB |
Output is correct |
2 |
Correct |
19 ms |
348 KB |
Output is correct |
3 |
Correct |
17 ms |
600 KB |
Output is correct |
4 |
Correct |
16 ms |
348 KB |
Output is correct |
5 |
Correct |
19 ms |
588 KB |
Output is correct |
6 |
Correct |
5 ms |
348 KB |
Output is correct |
7 |
Correct |
7 ms |
344 KB |
Output is correct |
8 |
Correct |
5 ms |
348 KB |
Output is correct |
9 |
Correct |
5 ms |
584 KB |
Output is correct |
10 |
Correct |
18 ms |
604 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
348 KB |
Output is correct |
2 |
Correct |
19 ms |
348 KB |
Output is correct |
3 |
Correct |
17 ms |
600 KB |
Output is correct |
4 |
Correct |
16 ms |
348 KB |
Output is correct |
5 |
Correct |
19 ms |
588 KB |
Output is correct |
6 |
Correct |
5 ms |
348 KB |
Output is correct |
7 |
Correct |
7 ms |
344 KB |
Output is correct |
8 |
Correct |
5 ms |
348 KB |
Output is correct |
9 |
Correct |
5 ms |
584 KB |
Output is correct |
10 |
Correct |
18 ms |
604 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Incorrect |
18 ms |
348 KB |
Output isn't correct |
15 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
18 ms |
588 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
600 KB |
Output is correct |
2 |
Correct |
1 ms |
856 KB |
Output is correct |
3 |
Correct |
1 ms |
604 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
1 ms |
604 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 |
604 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
0 ms |
348 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 |
604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
348 KB |
Output is correct |
2 |
Correct |
19 ms |
348 KB |
Output is correct |
3 |
Correct |
17 ms |
600 KB |
Output is correct |
4 |
Correct |
16 ms |
348 KB |
Output is correct |
5 |
Correct |
19 ms |
588 KB |
Output is correct |
6 |
Correct |
5 ms |
348 KB |
Output is correct |
7 |
Correct |
7 ms |
344 KB |
Output is correct |
8 |
Correct |
5 ms |
348 KB |
Output is correct |
9 |
Correct |
5 ms |
584 KB |
Output is correct |
10 |
Correct |
18 ms |
604 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Incorrect |
18 ms |
348 KB |
Output isn't correct |
15 |
Halted |
0 ms |
0 KB |
- |