#include<bits/stdc++.h>
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("trapv")
#define st first
#define nd second
#define pb push_back
#define pp pop_back
#define eb emplace_back
#define mp(a, b) make_pair(a, b)
#define all(x) (x).begin(), (x).end()
#define rev(x) reverse(all(x))
#define sor(x) sort(all(x))
#define sz(x) (int)(x).size()
#define rsz(x) resize(x)
using namespace std;
///~~~~~~~~~~~~~~~~~~~~~~~~~~
template <typename H, typename T>
ostream& operator<<(ostream& os, pair<H, T> m){
return os <<"("<< m.st<<", "<<m.nd<<")";
}
template <typename H>
ostream& operator<<(ostream& os, vector<H> V){
os<<"{";
for(int i=0; i<V.size(); i++){
if(i)os<<" ";
os<<V[i];
}
os<<"}";
return os;
}
void debug(){cerr<<"\n";}
template <typename H, typename... T>
void debug(H h, T... t) {cerr<<h; if (sizeof...(t)) cerr << ", "; debug(t...);}
#define deb(x...) cerr<<#x<<" = ";debug(x);
///~~~~~~~~~~~~~~~~~~~~~~~~~
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<pii > vii;
typedef vector<ll> vl;
typedef vector<pll> vll;
typedef string str;
#define BOOST ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
const int N=1e6+5, INF=1e9+5, mod=1e9+7;
vi id[N], id2[N], mini[N];//id-kto ma dp o takiej wartosci, mini-najmniejsza najwieksza druzyna
int dp[N], gdzie[N], par[N], opt[N], mi[N];//dp-ile druz, gdzie-numer w wektorze, par-rodzic, opt-maxi na przedziale
int main(){
BOOST;
int n;
cin>>n;
vii V(n+1);
for(int i=1; i<=n; i++){
cin>>V[i].st;
V[i].nd=i;
}
sor(V);
id[0].pb(0);
for(int i=1; i<=n; i++){
//deb(i);
int j=i-V[i].st;
if(j<0){
opt[i]=opt[i-1];
}
else{
//deb(i);
int k=opt[j];
dp[i]=k+1;
opt[i]=max(opt[i-1], k+1);
id[k+1].pb(i);
}
//deb(dp[i]);
}
for(int k=0; k<dp[n]; k++){
vii spec;
for(int j:id[k+1])spec.eb(j-V[j].st, j);
sor(spec);
rev(spec);
id[k].pb(n+1);
for(int j:id[k]){
while(spec.size() && spec.back().st<j){
int i=spec.back().nd;
int l=0, r=id2[k].size();
while(l<r){
int m=(l+r)>>1;
if(i-m>=mini[k][m])l=m+1;
else r=m;
}
//deb(i);
int m;
if(l==id2[k].size() || id2[k][l]>j || (l!=0 && i-l+1>mini[k][l])){
par[i]=id2[k][l-1];
m=i-l+1;
}
else{
par[i]=id2[k][l];
m=mini[k][l];
}
mi[i]=m;
spec.pp();
}
if(j==n+1)break;
while(id2[k].size() && mi[j]<=mini[k].back()){
id2[k].pp();
mini[k].pp();
}
id2[k].pb(j);
mini[k].pb(mi[j]);
}
}
for(int i=1; i<=n; i++){
//deb(i, dp[i], par[i], mi[i]);
}
cout<<dp[n]<<"\n";
int v=n;
while(v!=0){
int v2=par[v];
cout<<v-v2<<" ";
for(int i=v; i>v2; i--){
cout<<V[i].nd<<" ";
}
cout<<"\n";
v=v2;
}
}
Compilation message
tea.cpp: In function 'int main()':
tea.cpp:104:9: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
104 | if(l==id2[k].size() || id2[k][l]>j || (l!=0 && i-l+1>mini[k][l])){
| ~^~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
37 ms |
70736 KB |
Output is correct |
2 |
Correct |
34 ms |
70716 KB |
Output is correct |
3 |
Incorrect |
34 ms |
70740 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
36 ms |
70736 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
33 ms |
70740 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
38 ms |
71036 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
38 ms |
70968 KB |
Output is correct |
2 |
Incorrect |
36 ms |
71024 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
58 ms |
73880 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
71 ms |
74340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
256 ms |
101836 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
340 ms |
109704 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
323 ms |
107136 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |