QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#805999 | #9865. Dolls | ucup-team135 | TL | 808ms | 9732kb | C++20 | 6.4kb | 2024-12-08 20:22:07 | 2024-12-08 20:22:07 |
Judging History
answer
#pragma GCC optimize("Ofast")
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <ctime>
#include <cassert>
#include <complex>
#include <string>
#include <cstring>
#include <chrono>
#include <random>
#include <bitset>
#include <fstream>
#include <array>
#include <functional>
#include <stack>
#include <memory>
using namespace std;
#define ii pair <int, int>
#define app push_back
#define bp __builtin_popcountll
#define mp make_pair
#define x first
#define y second
#define Time (double)clock()/CLOCKS_PER_SEC
#define munq(a) sort(all(a)); a.resize(unique(all(a))-a.begin())
#define sz(a) ((int)a.size())
const int p=998244353;
int po(int a,int b) {if(b==0) return 1; if(b==1) return a; if(b%2==0) {int u=po(a,b/2);return (u*1LL*u)%p;} else {int u=po(a,b-1);return (a*1LL*u)%p;}}
int inv(int x) {return po(x,p-2);}
mt19937 rnd;
#define all(x) (x).begin(),(x).end()
#ifdef LOCAL
#define debug(...) [](auto...a){ ((cout << a << ' '), ...) << endl;}(#__VA_ARGS__, ":", __VA_ARGS__)
#define debugv(v) do {cout<< #v <<" : {"; for(int izxc=0;izxc<v.size();++izxc) {cout << v[izxc];if(izxc+1!=v.size()) cout << ","; }cout <<"}"<< endl;} while(0)
#else
#define debug(...)
#define debugv(v)
#endif
#define lob(a,x) lower_bound(all(a),x)
#define upb(a,x) upper_bound(all(a),x)
int sz;
const int maxn=(1<<17);
const int inf=1e9;
int a[maxn];int rr[maxn];int ll[maxn];int rr2[maxn];int ll2[maxn];
int t[4*maxn];int t2[4*maxn];
void to(int node,int tl,int tr,int pos,int val)
{
if(tr-tl==1)
{
t[node]=val;
return;
}
int tm=(tl+tr)/2;
if(pos<tm) {to(2*node+1,tl,tm,pos,val);} else {to(2*node+2,tm,tr,pos,val);}
t[node]=min(t[2*node+1],t[2*node+2]);
}
int get(int node,int tl,int tr,int l,int r)
{
if(tl>=r || tr<=l) return inf;
if(tl>=l && tr<=r) {return t[node];}
int tm=(tl+tr)/2;
return min(get(2*node+1,tl,tm,l,r),get(2*node+2,tm,tr,l,r));
}
void to2(int node,int tl,int tr,int pos,int val)
{
if(tr-tl==1)
{
t2[node]=val;
return;
}
int tm=(tl+tr)/2;
if(pos<tm) {to2(2*node+1,tl,tm,pos,val);} else {to2(2*node+2,tm,tr,pos,val);}
t2[node]=min(t2[2*node+1],t2[2*node+2]);
}
int get2(int node,int tl,int tr,int l,int r)
{
if(tl>=r || tr<=l) return inf;
if(tl>=l && tr<=r) {return t2[node];}
int tm=(tl+tr)/2;
return min(get2(2*node+1,tl,tm,l,r),get2(2*node+2,tm,tr,l,r));
}
vector<pair<int,int> > rec;
void go(int l,int r)
{
int m=(l+r)/2;
if(r-l<4) return;
if(r-l!=1) {go(l,m);go(m,r);}
{
for(int i=m;i<r;++i)
{
to(0,0,sz,a[i],i);
}
rec.clear();
for(int i=m-1;i>=l;--i)
{
int pos=upper_bound(all(rec),make_pair(a[i],i))-rec.begin();
if(pos!=rec.size())
{
int three=i;
int four=rec[pos].second;
int valone=get2(0,0,sz,three,four);
if(valone>=a[three]) {continue;}
int two=get(0,0,sz,valone,a[three]);
//if(three==0 && two==2) {debug(a[0],a[1],a[2],a[3]);debug(valone);}
rr[three]=min(rr[three],two-1);
}
if(rec.empty() || a[i]>=rec.back().first) {rec.app({a[i],i});}
}
for(int i=m;i<r;++i)
{
to(0,0,sz,a[i],inf);
}
}
{
vector<pair<int,int> > zxc1;
int val=inf;
for(int i=m-1;i>=l;--i)
{
zxc1.app({i,val});
val=min(val,a[i]);
}
vector<pair<int,int> > zxc2;
val=(-inf);
for(int i=m;i<r;++i)
{
zxc2.app({val,i});
val=max(val,a[i]);
}
sort(all(zxc1),[&](pair<int,int> i1,pair<int,int> j1) {return a[i1.first]>a[j1.first];});
sort(all(zxc2));reverse(all(zxc2));
int cur=0;
//for(int i=0;i<4*sz;++i) assert(t[i]==inf);
for(auto h:zxc1)
{
auto [i,mi]=h;
if(mi>=a[i]) {continue;}
while(cur<zxc2.size() && zxc2[cur].first>a[i])
{
int j=zxc2[cur].second;
if(zxc2[cur].first<=a[j]) {++cur;continue;}
//debug(a[j],j,zxc2[cur].first);
to(0,0,sz,a[j],j);
++cur;
}
/*if(i==0 && get(0,0,sz,mi,a[i])==3)
{
debug(a[0],a[1],a[2],a[3],i,mi,m);
}*/
rr[i]=min(rr[i],get(0,0,sz,mi,a[i])-1);
}
for(int i=m;i<r;++i)
{
to(0,0,sz,a[i],inf);
}
}
}
int32_t main()
{
ios_base::sync_with_stdio(false);cin.tie(0);
int te;cin>>te;
while(te--) {
int n;cin>>n;int n1=n;
for(int i=0;i<n;++i) {cin>>a[i];--a[i];}
while(__builtin_popcount(n)!=1)
{
a[n]=n;++n;
}
sz=n;
fill(t,t+4*sz,inf);fill(t2,t2+4*sz,inf);
for(int i=0;i<n;++i) {rr[i]=n-1;ll[i]=0;}
{
for(int i=0;i<n;++i) {to2(0,0,sz,i,a[i]);}
go(0,n);
}
for(int i=0;i<n;++i) {a[i]=n-1-a[i];}
{
for(int i=0;i<n;++i) {to2(0,0,sz,i,a[i]);}
go(0,n);
}
//debug(rr[0]);
copy(rr,rr+sz,rr2);fill(rr,rr+sz,n-1);
copy(ll,ll+sz,ll2);fill(ll,ll+sz,0);
reverse(a,a+n);
for(int i=0;i<n;++i) {rr[i]=n-1;ll[i]=0;}
{
for(int i=0;i<n;++i) {to2(0,0,sz,i,a[i]);}
go(0,n);
}
for(int i=0;i<n;++i) {a[i]=n-1-a[i];}
{
for(int i=0;i<n;++i) {to2(0,0,sz,i,a[i]);}
go(0,n);
}
reverse(a,a+n);
//for(int i=0;i<n;++i) {debug(i,ll[i],rr[i]);}
for(int i=0;i<n;++i)
{
ll2[i]=max(ll2[i],n-1-rr[n-1-i]);
rr2[i]=min(rr2[i],n-1-ll[n-1-i]);
}
copy(ll2,ll2+sz,ll);copy(rr2,rr2+sz,rr);
for(int i=0;i<n;++i)
{
if(ll[i])
{
rr[ll[i]-1]=min(rr[ll[i]-1],i-1);
}
}
for(int i=n-1;i>=1;--i)
{
rr[i-1]=min(rr[i-1],rr[i]);
}
//for(int i=0;i<n;++i) {debug(i,ll[i],rr[i]);}
int dp[n+1]={0};fill(dp+1,dp+n+1,inf);dp[0]=0;
for(int i=0;i<n;++i)
{
dp[rr[i]+1]=min(dp[rr[i]+1],dp[i]+1);
}
int res=0;
for(int i=n1;i<=n;++i)
{
res=max(res,n1-dp[i]);
}
cout<<res<<'\n';
}
return 0;
}
/*
1
5
1 4 2 5 3
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 7652kb
input:
8 4 2 1 4 3 4 1 4 2 3 4 3 1 4 2 5 1 3 5 2 4 5 1 4 2 5 3 5 2 5 3 1 4 6 1 3 6 5 2 4 6 2 5 1 3 6 4
output:
3 3 2 3 3 3 4 4
result:
ok 8 numbers
Test #2:
score: 0
Accepted
time: 25ms
memory: 7900kb
input:
5913 1 1 2 1 2 2 2 1 3 1 2 3 3 1 3 2 3 2 1 3 3 2 3 1 3 3 1 2 3 3 2 1 4 1 2 3 4 4 1 2 4 3 4 1 3 2 4 4 1 3 4 2 4 1 4 2 3 4 1 4 3 2 4 2 1 3 4 4 2 1 4 3 4 2 3 1 4 4 2 3 4 1 4 2 4 1 3 4 2 4 3 1 4 3 1 2 4 4 3 1 4 2 4 3 2 1 4 4 3 2 4 1 4 3 4 1 2 4 3 4 2 1 4 4 1 2 3 4 4 1 3 2 4 4 2 1 3 4 4 2 3 1 4 4 3 1 2 4...
output:
0 1 1 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3 2 3 3 2 3 3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4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 6 8 6 7 7 7 7 7 7 7 7 6 7 6 7 7 7 7 7 7 7 7 8 ...
result:
ok 10000 numbers
Test #9:
score: 0
Accepted
time: 145ms
memory: 7608kb
input:
10000 10 6 5 10 7 2 4 8 9 3 1 10 1 5 4 2 8 9 3 10 7 6 10 10 1 9 7 4 5 2 3 6 8 10 6 3 10 4 1 8 9 7 5 2 10 1 5 9 8 10 4 2 3 7 6 10 1 3 9 6 10 8 4 2 5 7 10 3 10 1 2 9 7 6 5 4 8 10 3 8 2 9 4 5 10 1 6 7 10 8 5 1 6 7 9 4 10 3 2 10 1 8 6 9 7 5 10 2 4 3 10 3 5 8 2 6 4 9 7 1 10 10 10 2 4 3 9 8 5 6 7 1 10 9 6...
output:
8 8 9 7 8 8 8 7 8 8 8 9 8 8 8 8 8 8 8 8 8 8 7 7 7 8 8 8 8 7 9 8 8 7 7 8 8 7 7 8 8 8 8 8 8 8 7 7 8 7 7 8 8 8 7 9 8 8 8 8 8 8 8 8 8 8 8 8 8 9 8 8 7 7 8 9 8 8 8 8 7 9 8 8 8 7 7 8 7 8 8 8 8 8 8 8 8 7 8 7 8 8 8 7 8 8 8 8 8 7 8 8 8 8 8 8 8 7 8 8 7 8 8 8 8 8 8 8 8 8 9 7 8 8 8 7 7 7 9 8 8 8 7 8 8 7 8 7 8 8 ...
result:
ok 10000 numbers
Test #10:
score: 0
Accepted
time: 441ms
memory: 7728kb
input:
1000 100 36 19 15 23 80 24 92 12 63 82 17 71 52 53 62 37 30 5 87 14 27 42 47 38 67 39 40 77 6 11 22 58 83 26 86 50 64 54 81 89 60 85 74 55 96 100 2 32 75 49 93 51 41 57 68 10 3 95 79 21 98 69 99 20 56 91 59 76 28 94 66 44 46 70 43 97 7 16 48 29 84 61 9 65 13 31 34 45 33 1 73 72 78 35 88 90 4 25 8 18...
output:
83 82 85 82 82 81 82 82 83 82 81 82 81 83 83 80 83 84 83 84 80 81 80 81 80 82 84 82 84 84 84 83 84 83 84 82 82 86 82 82 83 82 80 82 82 81 81 82 80 80 83 81 83 82 85 83 84 84 83 81 82 81 80 84 84 81 82 83 84 84 84 82 82 83 83 82 82 84 81 80 80 82 81 82 84 84 79 83 83 82 84 81 81 81 84 84 85 83 84 82 ...
result:
ok 1000 numbers
Test #11:
score: 0
Accepted
time: 808ms
memory: 7588kb
input:
100 1000 550 971 302 95 28 284 617 922 674 216 841 488 304 342 88 271 306 556 106 206 22 722 319 730 603 112 877 59 910 921 490 973 35 323 495 9 507 869 834 542 391 86 359 69 837 830 498 645 852 974 790 766 255 98 269 231 452 720 728 925 652 214 91 484 878 592 217 763 487 400 868 66 328 195 923 955 ...
output:
822 826 827 836 825 819 833 828 819 825 826 829 825 828 827 823 826 832 827 822 826 833 824 827 819 821 819 824 830 815 833 831 822 828 835 829 827 824 836 829 822 833 823 830 823 825 823 828 835 827 823 821 831 826 826 822 826 822 831 836 825 829 832 833 825 830 828 822 827 820 831 840 826 827 830 ...
result:
ok 100 numbers
Test #12:
score: -100
Time Limit Exceeded
input:
10 10000 4850 5255 5139 5540 1874 1076 4021 6824 4366 2054 2715 278 8256 4808 9269 3125 6278 690 6792 1562 3953 8690 6144 7653 8183 215 8338 6985 2329 6752 4704 3988 4919 9621 7203 1326 1144 8757 8689 7857 5536 9109 6881 1575 9834 8480 3599 2264 6399 7509 1483 6113 4446 9198 3827 5026 9359 1920 2 58...