QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#218794 | #5035. foo~ | chenxinyang2006 | 0 | 0ms | 0kb | C++14 | 4.1kb | 2023-10-18 18:35:10 | 2023-10-18 18:35:10 |
answer
#include <bits/stdc++.h>
#define rep(i,j,k) for(int i=(j);i<=(k);i++)
#define per(i,j,k) for(int i=(j);i>=(k);i--)
#define uint unsigned int
#define ll long long
#define ull unsigned long long
#define db double
#define ldb long double
#define pii pair<int,int>
#define pll pair<ll,ll>
#define mkp make_pair
#define eb emplace_back
#define SZ(S) (int)S.size()
//#define mod 998244353
//#define mod 1000000007
#define inf 0x3f3f3f3f
#define linf 0x3f3f3f3f3f3f3f3f
using namespace std;
template <class T>
void chkmax(T &x,T y){
if(x < y) x = y;
}
template <class T>
void chkmin(T &x,T y){
if(x > y) x = y;
}
inline int popcnt(int x){
return __builtin_popcount(x);
}
inline int ctz(int x){
return __builtin_ctz(x);
}
/*ll power(ll p,int k = mod - 2){
ll ans = 1;
while(k){
if(k % 2 == 1) ans = ans * p % mod;
p = p * p % mod;
k /= 2;
}
return ans;
}*/
int n,k;
int a[600005],b[600005],_pos[600005];
vector <int> sta;
vector <pii> Mx;
int f[600005],g[600005];
int fa[600005],_l[600005],_r[600005],val[600005],delta[600005];
int fnd(int u){
if(fa[u] == u) return u;
return fa[u] = fnd(fa[u]);
}
int mrg(int u,int v){
if(_r[u] - _l[u] > _r[v] - _l[v]){
swap(u,v);
swap(val[u],val[v]);
swap(delta[u],delta[v]);
}
fa[v] = u;
chkmin(_l[u],_l[v]);
chkmax(_r[u],_r[v]);
delta[u] += delta[v];
// cerr << "mrg " << u << " " << v << endl;
// Sleep(1000);
// cerr << val[u] << endl;
return u;
}
int answer,idx;
void push(int pos){
val[pos] = g[pos - 1] - (pos - 1);
// cerr << "PUSH " << pos << " " << val[pos] << endl;
int u = fnd(pos - 1);
while(val[u] + delta[u] <= val[pos]) u = mrg(fnd(_l[u] - 1),u);
if(!_l[u]){
answer = val[pos];
idx = pos;
}
}
void add(int pos,int lim){//pos 后缀加 1
// cerr << "add " << pos << " lim=" << lim << endl;
if(idx < pos) answer--;
pos--;//[1,pos] 减 1
int u = fnd(pos),nxt = fnd(_r[u] + 1);
delta[u]--;
if(nxt > lim) return;
while(val[u] + delta[u] <= val[nxt]){
if(idx == _l[u]){
answer += val[nxt] - val[u] - delta[u];
idx = _l[nxt];
}
u = mrg(fnd(_l[u] - 1),u);
}
}
void debug(int lim){
rep(i,1,lim) if(fa[i] == i) cerr << i << " l=" << _l[i] << " r=" << _r[i] << " val=" << val[i] << " delta=" << delta[i] << endl;
cerr << "answer=" << answer << " idx=" << idx << endl;
}
void trans(){
val[0] = inf;
sta.clear();Mx.clear();
rep(i,1,n){
int cur = -inf;
while(!sta.empty() && a[i] > a[sta.back()]){
chkmax(cur,Mx.back().first);
sta.pop_back();
Mx.pop_back();
}
chkmax(cur,g[i - 1]);
if(!Mx.empty()) Mx.eb(mkp(cur,max(Mx.back().second,cur - SZ(sta))));
else Mx.eb(mkp(cur,cur));
sta.eb(i);
f[i] = Mx.back().second + SZ(sta);
// printf("pos=%d\n",i);
// for(int p:sta) printf("%d ",p);
// printf("\n");
// for(pii I:Mx) printf("(%d,%d) ",I.first,I.second);
// printf("\n");
}
rep(i,0,n){
fa[i] = _l[i] = _r[i] = i;
delta[i] = 0;
}
answer = 0;
rep(i,1,n){
push(i);
add(_pos[i] + 1,i);
chkmax(f[i],answer + i);
// cerr << "result= " << answer << endl;
// debug(i);
}
rep(i,1,n) g[i] = f[i];
// rep(i,1,n) cerr << g[i] << " ";
// cerr << endl;
}
int solve(){
// rep(i,1,n) cerr << a[i] << " ";
// cerr << endl;
sta.clear();
g[0] = -inf;
rep(i,1,n){
while(!sta.empty() && a[i] > a[sta.back()]) sta.pop_back();
if(sta.empty()) _pos[i] = 0;
else _pos[i] = sta.back();
sta.eb(i);
g[i] = SZ(sta);
}
/* rep(i,1,n) printf("%d ",g[i]);
printf("\n");
printf("_pos:\n");
rep(i,1,n) printf("%d ",_pos[i]);
printf("\n");*/
rep(i,1,k - 1) trans();
int ans = 0;
rep(i,1,n) chkmax(ans,g[i]);
// printf("%d\n",ans);
return ans;
}
int main(){
freopen("test.in","r",stdin);
freopen("test.out","w",stdout);
scanf("%d%d",&n,&k);
int pos = -1;
rep(i,1,n){
scanf("%d",&b[i]);
if(b[i] == n) pos = i;
}
assert(pos != -1);
rep(i,pos,n) a[i - pos + 1] = b[i];
rep(i,1,pos - 1) a[n - pos + 1 + i] = b[i];
int ans = solve();
reverse(a + 2,a + n + 1);
printf("%d\n",max(ans,solve()));
// printf("%d\n",ans);
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 0
Dangerous Syscalls
Test #1:
score: 0
Dangerous Syscalls
input:
23 6 16 20 22 4 21 10 3 7 5 8 15 12 9 1 6 17 23 13 11 19 18 14 2
output:
result:
Subtask #2:
score: 0
Dangerous Syscalls
Test #21:
score: 0
Dangerous Syscalls
input:
93943 1 87243 48984 50611 19218 77699 25025 85769 28141 13380 34875 42459 66419 53472 4367 48292 16894 92171 87263 42527 67085 30687 29235 27515 81053 31421 34864 83591 70491 75344 7026 50250 63402 26773 5330 36308 76399 80032 15501 16205 71750 73964 67876 68901 70548 2043 79979 89479 19784 38838 44...
output:
result:
Subtask #3:
score: 0
Dangerous Syscalls
Test #36:
score: 0
Dangerous Syscalls
input:
1992 25 144 612 1315 1966 1779 1773 1529 625 36 1849 1783 1441 1388 1558 1258 724 137 397 542 353 1162 1213 406 792 1317 882 994 298 1864 1969 103 449 508 1501 89 1721 195 778 657 222 1152 1780 613 743 1206 694 829 142 69 1973 1465 1343 655 1540 155 146 350 491 759 1695 1082 1357 1329 1745 232 1850 ...
output:
result:
Subtask #4:
score: 0
Skipped
Dependency #1:
0%
Subtask #5:
score: 0
Skipped
Dependency #4:
0%
Subtask #6:
score: 0
Skipped
Dependency #2:
0%