hdu5491 the next 贪心水题

补了下去年网络赛的题。。。很无语的感觉。。。。

这道签到去年做了4个小时,WA了十次。。。。后来还是队友过的,赛后由于网络赛打得太惨,也没敢去补题。。。

现在再来看这题,,,好简单。。。。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#define REP(i,a,b) for(int i=a;i<=b;i++)
#define MS0(a) memset(a,0,sizeof(a))
#define key_val ch[ch[rt[i]][1]][0]

using namespace std;

typedef long long ll;
const int maxn=1000100;
const int INF=1e9+10;

ll d,s1,s2;

int cnt1(ll n)
{
    int res=0;
    while(n){
        res+=n%2;
        n>>=1;
    }
    return res;
}

ll solve(ll d)
{
    int one=cnt1(d);
    if(s1<=one&&one<=s2) return d;
    if(one<s1){
        int cnt=one;
        for(int i=0;i<s1;i++){
            if(cnt>=s1) break;
            if(d&(1<<i)) continue;
            else d|=(1<<i),cnt++;
        }
        return d;
    }
    else{
        for(int i=0;;i++){
            if(d&(1<<i)) return solve(d+(1<<i));
        }
    }
}

int main()
{
    #ifndef ONLINE_JUDGE
        freopen("in.txt","r",stdin);
    #endif
    int casen=1;
    int T;cin>>T;
    while(T--){
        scanf("%I64d%I64d%I64d",&d,&s1,&s2);
        d++;
        printf("Case #%d: %I64d
",casen++,solve(d));
    }
    return 0;
}
View Code
没有AC不了的题,只有不努力的ACMER!
原文地址:https://www.cnblogs.com/--560/p/5211032.html