QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#205127 | #7560. Computer Network | ucup-team1479# | WA | 1ms | 5916kb | C++14 | 4.3kb | 2023-10-07 14:56:30 | 2023-10-07 14:56:31 |
Judging History
answer
//prepare for coding{{{
//preparaton{{{
#include<bits/stdc++.h>
#define RELEASE
#ifdef RELEASE
#define DB(...) ;
#else
#define FILE
#ifdef FILE
#define DB(...) fprintf(stderr,__VA_ARGS__)
#else
#define DB printf
#endif
#endif
#define int long long
//#define MOD_OPERATOR
//#define CMP_OPERATOR
//#define GRP_OPERATOR
//}}}
//constants{{{
const int N=2000000,A=1000000000;
const int INF=0x3f3f3f3f;
//}}}
//std{{{
using namespace std;
//}}}
//input and output{{{
inline int Read(){
char c=getchar();
int res=0;
bool b=0;
while(c>'9'||c<'0')
b=(c=='-'),c=getchar();
while(c>='0'&&c<='9')
res=(res<<3)+(res<<1)+c-'0',c=getchar();
return b?-res:res;
}
//}}}
//other small functions{{{
//}}}
//variables{{{
int n;
struct NODE{
int a,b;
inline bool operator<(const NODE &x)const{
return a<x.a;
}
}a[N+10];
//}}}
//modulo operators{{{
#ifdef MOD_OPERATOR
const int MOD=998244353;
inline int Add(int a,int b){
return a+b<MOD?a+b:a+b-MOD;
}
inline int MAdd(int &x,int y){
return (x+=y)<MOD?x:x-=MOD;
}
inline int Mul(int a,int b){
return 1ll*a*b%MOD;
}
inline int MMul(int &x,int y){
return x=1ll*x*y%MOD;
}
inline int Pow(int x,int b=MOD-2){
int res=1;
while(b){
if(b&1)
MMul(res,x);
MMul(x,x);
b>>=1;
}
return res;
}
#endif
//}}}
//compare operators{{{
inline int Max(int a,int b){
return a<b?b:a;
}
inline int CMX(int &x,int y){
return x<y?x=y:x;
}
inline int Min(int a,int b){
return a>b?b:a;
}
inline int CMN(int &x,int y){
return x>y?x=y:x;
}
//}}}
//graph operators{{{
#ifdef GRP_OPERATOR
int te=1,head[N+10];
int s,t;
struct EDGE{
int n,t;
}e[N*2+10];
inline void Adde(int u,int v){
e[++te].n=head[u];
e[te].t=v;
head[u]=te;
e[++te].n=head[v];
e[te].t=u;
head[v]=te;
}
#endif
//}}}
//}}}
//solve{{{
inline bool Check(int x){
for(int i=1;i<n;++i)
if((a[i].a>>x)-a[i].b!=(a[i+1].a>>x)-a[i+1].b)
return 0;
return 1;
}
inline int BitCnt(int x){
int res=0;
while(x)
x^=x&-x,++res;
return res;
}
inline int Highbit(int x){
int res=-1;
while(x)
x>>=1,++res;
return res;
}
inline int Solve(int l,int r){
return l==r?BitCnt(l):Min(BitCnt(l>>Highbit(l^r))+1,BitCnt(l));
}
inline int Calc(int l,int r,int x,int y,int z){
return Solve(l,r)+y-((x+l)>>z);
}
//}}}
//main{{{
int td;
int ans=INF;
struct QJ{
int l,r;
inline void Init(int a,int b){
l=a,r=b;
if(l>r)
swap(l,r);
}
inline bool operator<(const QJ &x)const{
return l<x.l;
}
}qj[N+10];
signed main(){
#ifdef FILE
freopen(".in","r",stdin);
#endif
n=Read();
for(int i=1;i<=n;++i)
a[i].a=Read();
for(int i=1;i<=n;++i)
a[i].b=Read();
sort(a+1,a+1+n);
for(int i=1;i<n;++i)
if(a[i].b>a[i+1].b)
return puts("-1"),0;
for(int x=0;x<=30;++x){
bool ok=0;
td=0;
int s=(1<<x)-1;
int l=0,r=1<<x;
for(int i=1;i<n;++i){
if((a[i].a>>x)>a[i].b||(a[i+1].a>>x)>a[i+1].b){
ok=1;
break;
}
int da=(a[i+1].a>>x)-(a[i].a>>x),db=a[i+1].b-a[i].b;
/*
DB("%d %d\n",x,i);
DB("%d %d %d %d\n",da,db,a[i].a>>x,a[i+1].a>>x);
*/
if(da<db-1||(da==db-1&&(a[i].a&s)>=(a[i+1].a&s)))
return puts("-1"),0;
if(da>db+1||(da==db+1&&(a[i].a&s)<=(a[i+1].a&s))){
ok=1;
break;
}
if(da==db+1){
if((a[i].a>>x)+1>a[i].b){
ok=1;
break;
}
CMX(l,(1<<x)-(a[i].a&s));
CMN(r,(1<<x)-(a[i+1].a&s));
}
else if(da==db-1){
if((a[i+1].a>>x)+1>a[i+1].b){
ok=1;
break;
}
CMX(l,(1<<x)-(a[i+1].a&s));
CMN(r,(1<<x)-(a[i].a&s));
}
else if((a[i].a>>x)+1>a[i].b||(a[i+1].a>>x)+1>a[i+1].b){
CMN(r,(1<<x)-(a[i].a&s));
CMN(r,(1<<x)-(a[i+1].a&s));
}
else if((a[i+1].a&s)!=(a[i].a&s))
qj[++td].Init((1<<x)-(a[i+1].a&s),(1<<x)-(a[i].a&s));
//DB("%d %d %d %d %d\n",x,i,l,r,da);
}
if(ok)
continue;
if(l>=r)
continue;
sort(qj+1,qj+1+td);
bool oks=0;
int mr=l;
for(int i=1;i<=td&&mr<r;++i){
if(qj[i].l>mr)
CMN(ans,Calc(mr,qj[i].l-1,a[1].a,a[1].b,x)),oks=1;
CMX(mr,qj[i].r);
}
if(mr<r)
CMN(ans,Calc(mr,r-1,a[1].a,a[1].b,x)),oks=1;
//DB("%d\n",x);
if(oks)
return printf("%lld\n",ans+x),0;
}
puts("-1");
return 0;
}
//}}}
//《象》曰:泽灭木,大过。君子以独立不惧,遁世无闷。
详细
Test #1:
score: 100
Accepted
time: 1ms
memory: 5904kb
input:
5 1 2 3 4 5 6 6 6 6 7
output:
9
result:
ok 1 number(s): "9"
Test #2:
score: 0
Accepted
time: 0ms
memory: 5592kb
input:
3 2 3 4 1 2 3
output:
-1
result:
ok 1 number(s): "-1"
Test #3:
score: 0
Accepted
time: 1ms
memory: 5836kb
input:
2 65536 65537 1 2
output:
32
result:
ok 1 number(s): "32"
Test #4:
score: 0
Accepted
time: 1ms
memory: 5892kb
input:
1 0 28
output:
28
result:
ok 1 number(s): "28"
Test #5:
score: -100
Wrong Answer
time: 1ms
memory: 5916kb
input:
1 249912 43
output:
-249869
result:
wrong answer 1st numbers differ - expected: '26', found: '-249869'