#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
using vi = vector<int>;
using pii = pair<int, int>;
using vvi = vector<vi>;
#define sz(x) int(x.size())
const int INF = 100'000'000;
const int Z = (1<<20);
vector<int> segtree;
void set(int i, int v)
{
i += Z;
segtree[i] = v;
for(i >>= 1; i >= 1; i >>= 1) segtree[i] = max(segtree[i<<1], segtree[(i<<1) | 1]);
}
vector<pii> segtree2;
void set2(int i, int v)
{
// cerr << "\n\n" << "set2 " << i << ' ' << v << '\n';
i += Z;
segtree2[i].first = v;
for(i >>= 1; i >= 1; i >>= 1) segtree2[i] = max(segtree2[i<<1], segtree2[(i<<1) | 1]);
// cerr << "root = " << segtree2[1]
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;
cin >> n;
pii a[1+n];
for(int i = 1; i <= n; i++)
{
cin >> a[i].first;
a[i].second = i;
}
sort(a+1, a+n+1);
int t;
vi dp(1+n);
vi occ[1+n];
vi bit(1+n+1, -INF);
// bit[0 +1] = 0;
for(int j = 0 +1; j <= n +1; j += j&-j) bit[j] = 0;
occ[0].push_back(0);
for(int i = 1; i <= n; i++)
{
t = -INF;
if(a[i].first > i)
{
;
}
else
{
for(int j = i - a[i].first +1; j >= 0 +1; j -= j&-j)
t = max(t, bit[j]+1);
// cerr << i << " -> " << i - a[i].first << '\n';
for(int j = i +1; j <= n +1; j += j&-j)
bit[j] = max(bit[j], t);
}
dp[i] = t;
if(t != -INF) occ[dp[i]].push_back(i);
// cerr << i << " : " << t << '\n';
}
int team_count = dp[n];
// int Z = (1<<20);
int lo = 1, hi = n;
while(lo != hi)
{
int mxs = (lo+hi)/2;
segtree = vector<int>(Z<<1, -INF);
int l = 0;
int r = -1;
vi new_dp(1+n, -INF);
new_dp[0] = 0;
// cerr << "n = " << n << '\n';
for(int i = 1; i <= n; i++)
{
int nl = max(0, i-mxs);
int nr = max(-1, i - a[i].first);
if(nl > nr) continue;
while(r < nr)
{
r++;
set(r, new_dp[r]);
}
while(l > nl)
{
l--;
set(l, new_dp[l]);
}
while(r > nr)
{
set(r, -INF);
r--;
}
while(l < nl)
{
set(l, -INF);
l++;
}
new_dp[i] = max(-INF, segtree[1]+1);
}
if(new_dp[n] == team_count)
hi = mxs;
else
lo = mxs+1;
}
// cerr << team_count << ' ' << lo << '\n';
int mxs = lo;
// cerr << "\n\n\n\n\n";
// cerr << lo << ' ' << hi << " : " << mxs << '\n';
segtree2 = vector<pii>(Z<<1, {-INF, -1});
for(int i = 0; i <= n; i++) segtree2[Z+i].second = i;
int l = 0;
int r = -1;
vi new_dp(1+n, -INF);
new_dp[0] = 0;
vi prv(1+n, -1);
// cerr << "n = " << n << '\n';
for(int i = 1; i <= n; i++)
{
int nl = max(0, i-mxs);
int nr = max(-1, i - a[i].first);
// if(i-mxs > i - a[i]) con
// cerr << "i = " << i << " : " << "nl nr = " << nl << ' ' << nr << '\n';
if(nl > nr) continue;
// cerr << "journey " << l << ' ' << r << " -> " << nl << ' ' << nr << '\n';
while(r < nr)
{
r++;
set2(r, new_dp[r]);
}
while(l > nl)
{
l--;
set2(l, new_dp[l]);
}
while(r > nr)
{
set2(r, -INF);
r--;
}
while(l < nl)
{
set2(l, -INF);
l++;
}
// cerr << l << ' ' << r << '\n';
// cerr << "st = " << segtree2[1].first << ' ' << segtree2[1].second << '\n';
new_dp[i] = max(-INF, segtree2[1].first+1);
prv[i] = segtree2[1].second;
// cerr << i << " : " << new_dp[i] << ' ' << prv[i] << '\n';
}
vvi teams;
int I = n;
while(I != 0)
{
teams.push_back(vi(0));
// cerr << I << ' ' << prv[I] << '\n';
for(int q = I; q > prv[I]; q--)
teams.back().push_back(a[q].second);
I = prv[I];
}
cout << team_count << '\n';
for(int f = 0; f < team_count; f++)
{
cout << sz(teams[f]) << ' ';
for(int w: teams[f]) cout << w << ' ';
cout << '\n';
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
16632 KB |
Output is correct |
2 |
Correct |
30 ms |
33112 KB |
Output is correct |
3 |
Correct |
32 ms |
33172 KB |
Output is correct |
4 |
Correct |
24 ms |
33120 KB |
Output is correct |
5 |
Correct |
28 ms |
33148 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
33112 KB |
Output is correct |
2 |
Correct |
24 ms |
33068 KB |
Output is correct |
3 |
Correct |
27 ms |
33192 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
33152 KB |
Output is correct |
2 |
Correct |
27 ms |
33148 KB |
Output is correct |
3 |
Correct |
31 ms |
33164 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
46 ms |
33384 KB |
Output is correct |
2 |
Correct |
40 ms |
33332 KB |
Output is correct |
3 |
Correct |
40 ms |
33324 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
35 ms |
33340 KB |
Output is correct |
2 |
Correct |
36 ms |
33328 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
154 ms |
37004 KB |
Output is correct |
2 |
Correct |
228 ms |
37160 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
202 ms |
37556 KB |
Output is correct |
2 |
Correct |
184 ms |
37008 KB |
Output is correct |
3 |
Correct |
190 ms |
37568 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1746 ms |
72184 KB |
Output is correct |
2 |
Correct |
1808 ms |
77292 KB |
Output is correct |
3 |
Correct |
1570 ms |
76888 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2040 ms |
87460 KB |
Output is correct |
2 |
Execution timed out |
2584 ms |
161168 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1140 ms |
87368 KB |
Output is correct |
2 |
Correct |
300 ms |
86720 KB |
Output is correct |
3 |
Correct |
2310 ms |
88240 KB |
Output is correct |
4 |
Correct |
2136 ms |
92824 KB |
Output is correct |