hdu 5208 Where is Bob(数位dp)

题意:Alice和Bob各有一个选择数的区间,2人各选一个异或是最后的值,Alice想让这个数大,Bob想让这个数小,2人都很聪明,Alice先选,问最后的数是多少?

做法:可以知道如果要异或为1,那么必须一个为0,一个为1,因为Alice先选,所以若想那位为1,如果Alice这位选0,Bob的数必须这位都是1,才能使得这位为1。反之也一样,所以就从高位往低位走进行搜索,记得上下界是会随着选的数进行改变的。

当Bob的上界这位为1,下界这位为0的时候是需要朝2个方向进行搜索的,因而可能效率会非常低。所以这里需要一个非常优秀的减枝,就是当Bob的上下界包含了Alice的上下界,那么就不需要搜了,Alice后面无论怎么选Bob都能选相同的使得后面的位全为0。

我测了一下。当数据为1 500000000 500000000 1000000000时,如果不加减枝dfs要跑7*10^7次,加了只需要跑32次。不过我不知道该如何证明这个复杂度。。

AC代码:

//#pragma comment(linker, "/STACK:102400000,102400000")
#include<cstdio>
#include<ctype.h>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<vector>
#include<cstdlib>
#include<stack>
#include<queue>
#include<set>
#include<map>
#include<cmath>
#include<ctime>
#include<string.h>
#include<string>
#include<sstream>
#include<bitset>
using namespace std;
#define ll long long
#define ull unsigned long long
#define eps 1e-8
#define NMAX 100100
#define MOD 1000000007
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define PI acos(-1)
template<class T>
inline void scan_d(T &ret)
{
    char c;
    int flag = 0;
    ret=0;
    while(((c=getchar())<'0'||c>'9')&&c!='-');
    if(c == '-')
    {
        flag = 1;
        c = getchar();
    }
    while(c>='0'&&c<='9') ret=ret*10+(c-'0'),c=getchar();
    if(flag) ret = -ret;
}

int ans;
void dfs(int shu, int l1, int r1, int l2, int r2, int pos)
{
    if(pos == -1)
    {
        if(ans == 0 || shu > ans) ans = shu;
        return;
    }
    if(ans != 0 && shu+(1<<(pos+1))-1 < ans) return;
    if(l2 <= l1 && r2 >= r1)
    {
        if(ans == 0 || shu > ans) ans = shu;
        return;
    }
    if(l2 >= (1<<pos))
    {
        if(l1 < (1<<pos)) dfs(shu|(1<<pos),l1,r1 >= (1<<pos) ? (1<<pos)-1 : r1,l2^(1<<pos),r2^(1<<pos),pos-1);
        else dfs(shu,l1^(1<<pos),r1^(1<<pos),l2^(1<<pos),r2^(1<<pos),pos-1);
    }
    else if(r2 < (1<<pos))
    {
        if(r1 >= (1<<pos)) dfs(shu|(1<<pos),l1 >= (1<<pos) ? l1^(1<<pos) : 0, r1^(1<<pos),l2,r2,pos-1);
        else dfs(shu,l1,r1,l2,r2,pos-1);
    }
    else
    {
        if(r1 >= (1<<pos)) dfs(shu,l1 >= (1<<pos) ? l1^(1<<pos) : 0, r1^(1<<pos), 0, r2^(1<<pos), pos-1);
        if(l1 < (1<<pos)) dfs(shu,l1,r1 >= (1<<pos) ? (1<<pos)-1 : r1, l2,(1<<pos)-1,pos-1);
    }
}

int main()
{
#ifdef GLQ
    freopen("input.txt","r",stdin);
//    freopen("o1.txt","w",stdout);
#endif // GLQ
    int t,cas = 1;
    scanf("%d",&t);
    while(t--)
    {
        int x1,y1,x2,y2;
        scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
        ans = 0;
        dfs(0,x1,y1,x2,y2,30);
        printf("Case #%d: %d\n",cas++,ans);
    }
    return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值