hdu-5491 The Next(贪心)

题目链接:

The Next

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1766    Accepted Submission(s): 669

Problem Description

Let L denote the number of 1s in integer D’s binary representation. Given two integers S1 and S2, we call D a WYH number if S1≤L≤S2.
With a given D, we would like to find the next WYH number Y, which is JUST larger than D. In other words, Y is the smallest WYH number among the numbers larger than D. Please write a program to solve this problem.

Input

The first line of input contains a number T indicating the number of test cases (T≤300000).
Each test case consists of three integers D, S1, and S2, as described above. It is guaranteed that 0≤D<231 and D is a WYH number.

Output

For each test case, output a single line consisting of “Case #X: Y”. X is the test case number starting from 1. Y is the next WYH number.

Sample Input

3

11 2 4

22 3 3

15 2 5

Sample Output

Case #1: 12

Case #2: 25

Case #3: 17

题意:

给一个D,现在让求最小的ans>D,且ans的二进制中1的个数是[s1,s2];

思路:

套路题,从高位到低位按位贪心,对于当前位,如果为1,那么这一位就一定要取1,然后d,s1,s2都变小了;

如果当前位为0,那么就要看取最多个数的1能得到的数如果大于当前的数,那么还可以取0;

否则就要取1了;就这样贪心,具体的可以看代码;

AC代码:

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn=1e5+10;
int l,r;
LL d,ans,dp[40];
void dfs(LL cur,int hi,int lo,int pos)
{
    if(hi<=0||pos<0)return ;
    if(cur>=dp[pos]){ans+=dp[pos],dfs(cur-dp[pos],hi-1,lo-1,pos-1);}
    else
    {
        if(lo==pos+1)
        {
            ans+=dp[lo]-1;
            return ;
        }
        else
        {
            LL num=0;
            for(int i=pos-1;i>=max(0,pos-hi);i--)num+=dp[i];
            if(num>cur)dfs(cur,hi,lo,pos-1);
            else
            {
                ans+=dp[pos];
                lo--;
                if(lo>0)ans+=dp[lo]-1;
                return ;
            }
        }
    }
}
int main()
{
    int t,Case=0;
    scanf("%d",&t);
    dp[0]=1;
    for(int i=1;i<=33;i++)dp[i]=dp[i-1]*2;
    while(t--)
    {
        scanf("%lld%d%d",&d,&l,&r);
        ans=0;
        dfs(d,r,l,32);
        if(d==0)
        {
            if(l==0)ans=1;
            else ans=dp[l]-1;
        }
        printf("Case #%d: %lld\n",++Case,ans);
    }
    return 0;
}

  

时间: 2024-08-17 20:09:14

hdu-5491 The Next(贪心)的相关文章

hdu 4296 Buildings(贪心)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4296 Buildings Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1822    Accepted Submission(s): 722 Problem Description Have you ever heard the sto

hdu 4864 Task (贪心)

# include <stdio.h> # include <algorithm> # include <string.h> using namespace std; struct node { int t; int v; int yy; }; struct node a[100010],b[100010]; bool cmp(node a1,node a2) { if(a1.t==a2.t)//先按时间从大到小 return a1.v>a2.v;//再按水平从大

HDU 5014 Number Sequence 贪心 2014 ACM/ICPC Asia Regional Xi&#39;an Online

尽可能凑2^x-1 #include <cstdio> #include <cstring> const int N = 100005; int a[N], p[N]; int init(int x) { int cnt = 0; while(x > 1) { x /= 2; cnt ++; } return cnt + 1; } int main() { int n; while(~scanf("%d", &n)){ for(int i = 0;

HDU 4903 (模拟+贪心)

Fighting the Landlords Problem Description Fighting the Landlords is a card game which has been a heat for years in China. The game goes with the 54 poker cards for 3 players, where the “Landlord” has 20 cards and the other two (the “Farmers”) have 1

HDU 1045 Fire Net 贪心

Problem Description Suppose that we have a square city with straight streets. A map of a city is a square board with n rows and n columns, each representing a street or a piece of wall. A blockhouse is a small castle that has four openings through wh

HDU 1338 Game Prediction 贪心

Problem Description Suppose there are M people, including you, playing a special card game. At the beginning, each player receives N cards. The pip of a card is a positive integer which is at most N*M. And there are no two cards with the same pip. Du

hdu 1009 FatMouse&#39; Trade(贪心)

题目来源:hdu 1009 FatMouse' Trade FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 54581 Accepted Submission(s): 18299 Problem Description FatMouse prepared M pounds of cat food, ready

hdu 4925 Apple Tree(贪心)

http://acm.hdu.edu.cn/showproblem.php?pid=4925 尽量让每棵苹果树周围都施肥,每次找到一个空地种上苹果树之后,使其周围的空地施肥,不再种苹果树. #include <stdio.h> #include <iostream> #include <map> #include <set> #include <list> #include <stack> #include <vector>

hdu 4915 Parenthese sequence (贪心+模拟)

题目大意: 一个序列中有左括号和右括号,还有问号,问号可以任意转换成左右括号. 问这个序列有多少种情况的转变使得这个序列变成合法的括号匹配序列. 思路分析: 首先我们分析一下,如何使得一个序列是合法的括号匹配序列. 我们很容易想到的是用栈模拟匹配过程. 当遇到左括号就进栈,当遇到右括号就让栈顶的左括号出栈. 那么在模拟的过程中,造成这个序列的不合法的原因只有当右括号来的时候,此时的栈已经为空. 这里补充一句,一旦一个序列给定,那么这里面的问号有多少变成左括号,多少变成右括号,是一定的. 看完以上

HDU Crixalis&#39;s Equipment(贪心)

贪心策略: 数据(v1, v2),按 v2 - v1 的差值从大到小进行排序 分析: 例(1): v = 10 2件物品:a(2, 5) b(6, 8) -----如果先进a,再进b: 进a:v = 10 --> v >= 5 (成立) --> v -= 2 --> v = 8 进b:v = 8 --> v >= 8 (成立) --> v -= 6 --> v = 2 物品全部进入 输出Yes -----如果先进b,再进a: 进b:v = 10 -->