QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#187133 | #5150. Alternating Algorithm | Forever_Young# | WA | 1ms | 8076kb | C++23 | 2.3kb | 2023-09-24 14:50:01 | 2023-09-24 14:50:02 |
Judging History
answer
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,tune=native")
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=1;i<=n;++i)
#define per(i,n) for(int i=n;i>=1;--i)
#define pb push_back
#define mp make_pair
#define N 410000
int n;
int a[N];
int first[N];
bool cmp(int x,int y)
{
return a[x]<a[y];
}
struct node
{
int cnt,max,lazy;
}st[4*N];
void update(int t)
{
st[t].cnt=st[2*t].cnt+st[2*t+1].cnt;
st[t].max=max(st[2*t].max,st[2*t+1].max)+st[t].lazy;
}
void build(int t,int l,int r)
{
if (l==r)
{
st[t].cnt=1;
st[t].max=-10000000;
return;
}
int mid=(l+r)/2;
build(2*t,l,mid);
build(2*t+1,mid+1,r);
update(t);
}
void add(int t,int l,int r,int a,int b,int p)
{
if (a<=l && r<=b)
{
st[t].max+=p;
st[t].lazy+=p;
return;
}
int mid=(l+r)/2;
if (a<=mid)add(2*t,l,mid,a,b,p);
if (b>mid)add(2*t+1,mid+1,r,a,b,p);
update(t);
}
void setone(int t,int l,int r,int k)
{
if (l==r)
{
st[t].cnt=0;
st[t].max=l+first[l]-1+st[t].lazy;
return;
}
int mid=(l+r)/2;
if (k<=mid)setone(2*t,l,mid,k);
else setone(2*t+1,mid+1,r,k);
update(t);
}
int getfirst(int t,int l,int r)
{
if (st[t].cnt==0)return -1;
if (l==r)return l;
int mid=(l+r)/2;
if (st[2*t].cnt>0)return getfirst(2*t,l,mid);
else return getfirst(2*t+1,mid+1,r);
}
int main()
{
cin>>n;
for(int i=0;i<=n;i++)scanf("%d",&a[i]);
for(int i=0;i<=n;i++)
if (i&1)first[i]=0; else first[i]=1;
static int p[N];
for(int i=0;i<=n;i++)p[i]=i;
sort(p,p+n+1,cmp);
int ans=0;
int pre=0;
set<int> zero; zero.clear();
build(1,0,n); //set everyone as 1, and insert first[i]
for(int ii=1;ii<=n+1;ii++)
if (ii==n+1 || a[p[ii]]!=a[p[ii-1]])
{
int l=pre,r=ii-1;
pre=ii;
for(int i=l;i<=r;i++)
{
add(1,0,n,p[i]+1,n,-2);
setone(1,0,n,p[i]);
zero.insert(p[i]);
//one: p[i]+1..n 所有人-1
//zero: p[i]..n 所有人+1
}
if (zero.empty() || zero.size()==n+1)continue;
//线段树里定义st[i]为位置i的值
//st[i]=[1..i-1]里1的个数+first[i]-[1..i]前面0的个数
int t=getfirst(1,0,n); assert(t!=-1);
ans=max(ans,st[1].max+(int)zero.size()-t);
//答案是max(st[])+zero.size()-firstsegment
}
cout<<ans<<endl;
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 7740kb
input:
3 8 13 4 10
output:
3
result:
ok single line: '3'
Test #2:
score: 0
Accepted
time: 0ms
memory: 7832kb
input:
5 13 12 14 10 14 12
output:
3
result:
ok single line: '3'
Test #3:
score: 0
Accepted
time: 1ms
memory: 8060kb
input:
2 2 2 1
output:
3
result:
ok single line: '3'
Test #4:
score: 0
Accepted
time: 1ms
memory: 7832kb
input:
1 300172042 474444146
output:
0
result:
ok single line: '0'
Test #5:
score: 0
Accepted
time: 1ms
memory: 7784kb
input:
1 636357447 557539481
output:
1
result:
ok single line: '1'
Test #6:
score: 0
Accepted
time: 1ms
memory: 7844kb
input:
2 139715426 368724097 417561009
output:
0
result:
ok single line: '0'
Test #7:
score: -100
Wrong Answer
time: 1ms
memory: 8076kb
input:
2 77784868 542697475 509604021
output:
1
result:
wrong answer 1st lines differ - expected: '2', found: '1'