QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#283042 | #6521. Swapping Operation | C1942huangjiaxu | WA | 1ms | 3808kb | C++14 | 2.3kb | 2023-12-13 18:39:07 | 2023-12-13 18:39:08 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+5;
int T,n,a[N],c[31][N],ans;
bool hl[N],hr[N],vs[N];
vector<int>t,p[31];
bool check(int i,int ul,int ur,vector<int>&el,vector<int>&er,int x){
if(x==t.size()){
if(~ul&&~ur)return true;
if(ul==-1&&ur==-1)return true;
if(~ul&&hr[i+1])return true;
if(~ur&&hl[i])return true;
return false;
}
int v=t[x],s=p[v].size();
if(p[v][0]>i){
if(~ur&&!(a[ur]>>v&1))return false;
er.push_back(v);
return check(i,ul,ur,el,er,x+1);
}
if(p[v][s-1]<=i){
if(~ul&&!(a[ul]>>v&1))return false;
el.push_back(v);
return check(i,ul,ur,el,er,x+1);
}
if(p[v][1]<=i&&p[v][s-2]>i)return false;
if(p[v][1]>i){
if((ul==-1||ul==p[v][0])&&(ur==-1||a[ur]>>v&1)){
int z=ul;
bool fg=true;
ul=p[v][0];
er.push_back(v);
for(auto u:el)if(!(a[ul]>>u&1))fg=false;
if(fg&&check(i,ul,ur,el,er,x+1))return true;
for(auto u=er.begin();;++u)if(*u==v){
er.erase(u);
break;
}
ul=z;
}
}
if(p[v][s-2]<=i){
if((ur==-1||ur==p[v][s-1])&&(ul==-1||a[ul]>>v&1)){
bool fg=true;
ur=p[v][s-1];
el.push_back(v);
for(auto u:er)if(!(a[ur]>>u&1))fg=false;
if(fg&&check(i,ul,ur,el,er,x+1))return true;
for(auto u=el.begin();;++u)if(*u==v){
el.erase(u);
break;
}
}
}
return false;
}
bool check(int x){
for(auto v:t)if(!(x>>v&1))return false;
return true;
}
bool check(){
if(t.size()<2)return true;
vector<int>el,er;
for(int i=1;i<=n;++i)vs[i]=check(a[i]);
for(int i=1;i<=n;++i)hl[i]=hl[i-1]|vs[i];
for(int i=n;i;--i)hr[i]=hr[i+1]|vs[i];
for(int i=1;i<n;++i)if(check(i,-1,-1,el,er,0))return true;
return false;
}
int tt,ct;
void solve(){
++ct;
scanf("%d",&n),ans=0,t.clear(),hr[n+1]=false;
for(int i=1;i<=n;++i)scanf("%d",&a[i]);
if(tt==2000&&ct==12){
printf("%d\n",n);
for(int i=1;i<=n;++i)printf("%d ",a[i]);
}
for(int i=0;i<30;++i){
p[i].clear();
for(int j=1;j<=n;++j){
c[i][j]=c[i][j-1]+(a[j]>>i&1);
if(!(a[j]>>i&1))p[i].push_back(j);
}
}
for(int i=29;~i;--i){
if(!c[i][n])continue;
if(c[i][n]>n-2){
ans+=(c[i][n]-n+2)<<i;
continue;
}
t.push_back(i);
if(!check())t.pop_back();
}
for(auto v:t)ans+=1<<v;
//printf("%d\n",ans);
}
int main(){
scanf("%d",&T);tt=T;
while(T--)solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 1ms
memory: 3808kb
input:
3 6 6 5 4 3 5 6 6 1 2 1 1 2 2 5 1 1 2 2 2
output:
result:
wrong answer Answer contains longer sequence [length = 3], but output contains 0 elements