QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#204516#7560. Computer Networkucup-team870#TL 1ms7728kbC++141.9kb2023-10-07 12:40:072023-10-07 12:40:07

Judging History

你现在查看的是最新测评结果

  • [2023-10-07 12:40:07]
  • 评测
  • 测评结果:TL
  • 用时:1ms
  • 内存:7728kb
  • [2023-10-07 12:40:07]
  • 提交

answer

#include <bits/stdc++.h>
#define rep(i,l,r) for(int i=l; i<=r; i++)
#define per(i,r,l) for(int i=r; i>=l; i--)
#define IOS {cin.tie(0);cout.tie(0);ios::sync_with_stdio(0);}
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
#define pll pair<ll,ll>
const int N=1e6+6;
const ll inf=4e18;
int a[N],b[N];
pll q[N];
ll lb(ll x){return x&(-x);}
ll pp(ll l,ll r){
    ++r; 
    int res=66;
    while(1){
        ll v=lb(l);
        if(l+v>r)break;
        res=min(res,__builtin_popcountll(l));
        l+=v;
    }
    while(r){
        r-=lb(r);
        if(r<l)break;
        res=min(res,__builtin_popcountll(r));
    }
    return res;
}
int main() {
    IOS
    // rep(l,1,500){
    //     rep(r,l,500){
    //         int res=60; 
    //         rep(i,l,r)res=min(res,__builtin_popcountll(i));
    //         assert(res==pp(l,r));
    //     }
    // }
    // cout<<pp(1,10);
    // return 0;
    int n;cin>>n;
    rep(i,1,n)cin>>a[i];
    rep(i,1,n)cin>>b[i];
    ll ans=inf; int tot=0;
    rep(j,0,32){
        ll pw=1ll<<j;
        ll l=b[1]*pw,r=b[1]*pw+pw-1;
        rep(i,2,n){
            ll li=b[i]*pw,ri=b[i]*pw+pw-1;
            li-=a[i]-a[1]; ri-=a[i]-a[1];
            l=max(l,li); r=min(r,ri);
            if(l>r)break;
        }
        // cout<<j<<' '<<l<<' '<<r<<'\n';
        auto cal=[&](ll l,ll r){ //最小化 : v/2^j + pp(v%(2^j))
            ll vl=l/pw,vr=r/pw;
            if(vl==vr)return pp(l%pw,r%pw)+l/pw;
            ll v=vr*pw;
            assert(vr<=b[1]);
            return vr;
        };
        if(l<=r){
            if(r>=a[1]){
                ll res;
                if(a[1]>=l)res=0;
                else res=cal(l-a[1],r-a[1]);
                ans=min(ans,res+j); 
                if(++tot>=3)break;
            }
        }
    }
    if(ans==inf)cout<<"-1\n";
    else cout<<ans<<'\n';
}
/*
5
1 2 3 4 5
6 6 6 6 7
*/

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 7728kb

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: 1ms
memory: 7644kb

input:

3
2 3 4
1 2 3

output:

-1

result:

ok 1 number(s): "-1"

Test #3:

score: 0
Accepted
time: 1ms
memory: 7704kb

input:

2
65536 65537
1 2

output:

32

result:

ok 1 number(s): "32"

Test #4:

score: -100
Time Limit Exceeded

input:

1
0
28

output:


result: