QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#201974 | #5150. Alternating Algorithm | nameless_story | WA | 0ms | 14436kb | C++20 | 3.2kb | 2023-10-05 18:00:08 | 2023-10-05 18:00:09 |
Judging History
answer
#include"bits/stdc++.h"
using namespace std;
typedef long long ll;
template<class T1,class T2> bool cmin(T1 &x,const T2 &y) { if (y<x) { x=y; return 1; }return 0; }
template<class T1,class T2> bool cmax(T1 &x,const T2 &y) { if (x<y) { x=y; return 1; }return 0; }
#define all(x) (x).begin(),(x).end()
int n,m;
const int N=400005;
int a[N],p[N],b[N],vis[N];
pair<int,int> pa[N];
int len[N];
mt19937 rnd(2345);
int main()
{
ios::sync_with_stdio(0); cin.tie(0);
cout<<fixed<<setprecision(15);
int TT=1;
while (TT--)
{
memset(a,0,sizeof a);
memset(p,0,sizeof p);
memset(b,0,sizeof b);
memset(len,0,sizeof len);
memset(vis,0,sizeof vis);
memset(pa,0,sizeof pa);
cin>>n;
// n=5;
// iota(a,a+n+1,0);
// shuffle(a,a+n+1,rnd);
for (int i=0; i<=n; i++)
{
// a[i]=rnd()%100;
cin>>a[i];
}
int T=0;
/*static int A[N];
memcpy(A,a,sizeof A);
while (!is_sorted(A,A+n+1))
{
++T;
if (T&1)
{
for (int i=0; i<n; i+=2) sort(A+i,A+i+2);
}
else
{
for (int i=1; i<n; i+=2) sort(A+i,A+i+2);
}
}
memcpy(A,a,sizeof A);*/
for (int i=0; i<=n; i++) pa[i]={a[i],i};
sort(pa,pa+n+1);
vector vp(n+1,0);
iota(all(vp),0);
function<int(int)> getf=[&](int x)->int
{
return x==vp[x]?x:(vp[x]=getf(vp[x]));
};
for (int i=0; i<=n; i++)
{
// p[i]=getf(lower_bound(b,b+n+1,a[i])-b);
// vp[p[i]]=p[i]+1;
p[i]=pa[i].second;
// cerr<<p[i]<<" \n"[i==n];
}
fill(b,b+n+1,0);
set<int> S;
int ans=0;
for (int i=n; ~i; i--)
{
b[p[i]]=1;
auto nxt=S.upper_bound(p[i]);
if (nxt!=S.end())
{
if (*nxt-p[i]<=len[*nxt]||(len[*nxt]-1+(*nxt&1)>=(*nxt-len[*nxt]+1-p[i]+(p[i]&1))))
{
cmax(ans,(*nxt-len[*nxt]-p[i]+(p[i]&1)+1));
len[*nxt]++;
vp[p[i]]=getf(vp[*nxt]);
}
else
{
S.insert(p[i]);
len[p[i]]=1;
nxt=S.find(p[i]);
}
}
else
{
S.insert(p[i]);
len[p[i]]=1;
nxt=S.find(p[i]);
}
if (nxt==S.begin())
{
int tp=len[*S.begin()]-1+(*S.begin()&1),tm=i-(*S.begin()-len[*S.begin()]+1);
int tmp=tm==0?tm:tm+tp;
cmax(tmp,i-p[i]);
// cerr<<S.size()<<'\n';
// cerr<<p[i]<<' '<<*S.begin()<<' '<<len[*S.begin()]<<'\n';
// cerr<<tp<<' '<<tm<<'\n';
// cerr<<tmp<<"\n\n";
cmax(ans,tmp);
continue;
}
auto pre=nxt;
nxt--;
swap(nxt,pre);
// cerr<<"!!!"<<len[*nxt]-1+(*nxt&1)<<' '<<(*nxt-len[*nxt]+1-*pre+(*pre&1))<<'\n';
while (len[*nxt]-1+(*nxt&1)>=(*nxt-len[*nxt]+1-*pre+(*pre&1)))
{
cmax(ans,(*nxt-len[*nxt]-*pre+(*pre&1)-1+len[*pre]));
len[*nxt]+=len[*pre];
vp[getf(*pre)]=getf(*nxt);
S.erase(pre);
if (nxt==S.begin())
break;
pre=nxt;
nxt--;
swap(pre,nxt);
}
int tp=len[*S.begin()]-1+(*S.begin()&1),tm=i-(*S.begin()-len[*S.begin()]+1);
int tmp=tm==0?tm:tm+tp;
cmax(tmp,i-p[i]);
// cerr<<S.size()<<'\n';
// cerr<<p[i]<<' '<<*S.begin()<<' '<<len[*S.begin()]<<'\n';
// cerr<<tp<<' '<<tm<<'\n';
// cerr<<tmp<<"\n\n";
cmax(ans,tmp);
}
cout<<ans<<endl;
/*cerr<<T<<endl;
if (ans!=T)
{
for (int i=0; i<=n; i++) cerr<<A[i]<<" \n"[i==n];
}
assert(ans==T);*/
}
}
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 14360kb
input:
3 8 13 4 10
output:
3
result:
ok single line: '3'
Test #2:
score: 0
Accepted
time: 0ms
memory: 14388kb
input:
5 13 12 14 10 14 12
output:
3
result:
ok single line: '3'
Test #3:
score: 0
Accepted
time: 0ms
memory: 14356kb
input:
2 2 2 1
output:
3
result:
ok single line: '3'
Test #4:
score: -100
Wrong Answer
time: 0ms
memory: 14436kb
input:
1 300172042 474444146
output:
1
result:
wrong answer 1st lines differ - expected: '0', found: '1'