QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#187113 | #5150. Alternating Algorithm | Forever_Young# | WA | 2ms | 10096kb | C++23 | 2.4kb | 2023-09-24 14:42:16 | 2023-09-24 14:42:16 |
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-1)+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())continue;
auto p=zero.end(); p--;
int last=*p;
//线段树里定义st[i]为位置i的值
//st[i]=[1..i-1]里1的个数+first[i]-[1..i]前面0的个数
int t=getfirst(1,0,n);
if (t==-1)continue;
ans=max(ans,st[1].max+(int)zero.size()-(t-1));
//答案是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: 2ms
memory: 10084kb
input:
3 8 13 4 10
output:
3
result:
ok single line: '3'
Test #2:
score: 0
Accepted
time: 1ms
memory: 10096kb
input:
5 13 12 14 10 14 12
output:
3
result:
ok single line: '3'
Test #3:
score: 0
Accepted
time: 1ms
memory: 7768kb
input:
2 2 2 1
output:
3
result:
ok single line: '3'
Test #4:
score: 0
Accepted
time: 0ms
memory: 8048kb
input:
1 300172042 474444146
output:
0
result:
ok single line: '0'
Test #5:
score: 0
Accepted
time: 0ms
memory: 7780kb
input:
1 636357447 557539481
output:
1
result:
ok single line: '1'
Test #6:
score: 0
Accepted
time: 1ms
memory: 10068kb
input:
2 139715426 368724097 417561009
output:
0
result:
ok single line: '0'
Test #7:
score: -100
Wrong Answer
time: 1ms
memory: 7716kb
input:
2 77784868 542697475 509604021
output:
1
result:
wrong answer 1st lines differ - expected: '2', found: '1'