#include <bits/stdc++.h>
#define lsb(x) (x & (-x))
#define ll long long
#define ull unsigned long long
#define ld long double
// 217
// 44
using namespace std;
const int INF = 1e9;
const int MAXN = (int) 1e6;
pair <int, int> arr[MAXN + 1];
int dp[MAXN + 1], from[MAXN + 1];
int last[MAXN + 1];
inline int check(int n, int len) {
int mx = 0;
for(int i = 1; i <= n; i++) {
if(i - arr[i].first < 0) {
dp[i] = -INF;
}
else {
if(i - last[i - arr[i].first] <= len) {
dp[i] = dp[last[i - arr[i].first]] + 1;
from[i] = last[i - arr[i].first];
}
else {
dp[i] = -INF;
}
if(dp[i] < mx && i != n) {
dp[i] = -INF;
}
}
last[i] = last[i - 1];
if(dp[i] > -INF) {
last[i] = i;
}
mx = max(mx, dp[i]);
}
return dp[n];
}
int main() {
//ifstream cin("A.in");
//ofstream cout("A.out");
int i, n;
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> n;
for(i = 1; i <= n; i++) {
cin >> arr[i].first;
arr[i].second = i;
}
sort(arr + 1, arr + n + 1);
int ans = check(n, n);
int res = arr[n].first - 1;
for(int step = 1 << 20; step; step >>= 1) {
if(res + step <= n && check(n, res + step) < ans) {
res += step;
}
}
check(n, res + 1);
cout << ans << "\n";
int pos = n;
while(pos > 0) {
cout << pos - from[pos] << " ";
for(i = from[pos] + 1; i <= pos; i++) {
cout << arr[i].second << " ";
}
cout << "\n";
pos = from[pos];
}
//cin.close();
//cout.close();
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
372 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
504 KB |
Output is correct |
2 |
Correct |
3 ms |
504 KB |
Output is correct |
3 |
Correct |
3 ms |
504 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
504 KB |
Output is correct |
2 |
Correct |
3 ms |
500 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
25 ms |
2424 KB |
Output is correct |
2 |
Correct |
23 ms |
2452 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
29 ms |
2552 KB |
Output is correct |
2 |
Correct |
23 ms |
2296 KB |
Output is correct |
3 |
Correct |
28 ms |
2652 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
240 ms |
20088 KB |
Output is correct |
2 |
Correct |
227 ms |
20216 KB |
Output is correct |
3 |
Correct |
257 ms |
19904 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
346 ms |
26360 KB |
Output is correct |
2 |
Correct |
353 ms |
29560 KB |
Output is correct |
3 |
Correct |
325 ms |
26748 KB |
Output is correct |
4 |
Correct |
288 ms |
23544 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
318 ms |
25100 KB |
Output is correct |
2 |
Correct |
200 ms |
22936 KB |
Output is correct |
3 |
Correct |
317 ms |
26744 KB |
Output is correct |
4 |
Correct |
327 ms |
26696 KB |
Output is correct |