题目链接:

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;
}

  

 

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

  1. HDU 4442 Physical Examination(贪心)

    HDU 4442 Physical Examination(贪心) 题目链接http://acm.split.hdu.edu.cn/showproblem.php?pid=4442 Descripti ...

  2. 【贪心】【模拟】HDU 5491 The Next (2015 ACM/ICPC Asia Regional Hefei Online)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5491 题目大意: 一个数D(0<=D<231),求比D大的第一个满足:二进制下1个个数在 ...

  3. hdu 5491 The Next (位运算)

    http://acm.hdu.edu.cn/showproblem.php?pid=5491 题目大意:给定一个数D,它的二进制数中1的个数为L,求比D大的数的最小值x且x的二进制数中1的个数num满 ...

  4. HDU 5835 Danganronpa (贪心)

    Danganronpa 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5835 Description Chisa Yukizome works as ...

  5. HDU 5821 Ball (贪心)

    Ball 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5821 Description ZZX has a sequence of boxes nu ...

  6. hdu 5491(2015合肥网赛)The Next

    题目;http://acm.hdu.edu.cn/showproblem.php?pid=5491 题意就是,T组测试数据.然后L,S1,S2.L的二进制中有x个1,x满足  S1<=x< ...

  7. hdu 4004 (二分加贪心) 青蛙过河

    题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=4004 题目意思是青蛙要过河,现在给你河的宽度,河中石头的个数(青蛙要从石头上跳过河,这些石头都是在垂 ...

  8. Saving HDU(hdu2111,贪心)

    Saving HDU Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  9. HDU 4714 Tree2cycle:贪心

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4714 题意: 给你一棵树,添加和删除一条边的代价都是1.问你将这棵树变成一个环的最小代价. 题解: 贪 ...

  10. HDU 5303 Delicious Apples (贪心 枚举 好题)

    Delicious Apples Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Other ...

随机推荐

  1. IOS 局域网发送信息

    基于ios 例子WiTap 1.创建本地的服务并设置监听时间检测是否有设备连接. NSNetService * server = [[NSNetService alloc] initWithDomai ...

  2. 详细解读XMLHttpRequest(一)同步请求和异步请求

    本文主要参考:MDN XMLHttpRequest 让发送一个HTTP请求变得非常容易.你只需要简单的创建一个请求对象实例,打开一个URL,然后发送这个请求.当传输完毕后,结果的HTTP状态以及返回的 ...

  3. SharePoint 2010升级到sharePoint 2013后,人员失去对网站的权限的原因及解决方法。The reason and solution for permission lost after the upgrading

    昨天碰到了一个问题,一个网站在从SharePoint 2010升级到SharePoint 2013后,人员都不能登录了,必须重加赋权,人员才能登录,这样非常麻烦. 原因:是认证方式的问题.在Share ...

  4. Android Studio教程--从Github 下载一个Repository

    从电脑上彻底删除HelloWorld项目 回到as欢迎页面 复制github中项目的地址 点击clone

  5. java field, property,variable及getField和getDeclaredField的区别

    java 里面的field ,property, attribute,variable的区别 field: 就是定义的用于保存数据的字段 property: property是用于描述类中的特征,所以 ...

  6. 创建用户故事地图(User Story Mapping)的8个步骤

    [小编]上周六了解了用户故事地图后,小编又查阅了一些资料,找到了以下这篇关于如何组织用户故事地图规划的文章,分享给大家.也希望大家如果有好的实践,也可以留言一起交流. 原文地址:http://winn ...

  7. 谈一次php无限极分类的案例

    作者:白狼 出处:http://www.manks.top/php_tree_deep.html 本文版权归作者,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追 ...

  8. git操作之常见问题解决方案

    一.版本不一致 1. 错误信息: > git push -u origin master To ******.git ! [rejected] master -> master (non- ...

  9. Serena Dimensions 介绍

    Serena Dimensions是配置管理工具,基于进程的软件更改和配置管理解决方案. 官方网址:http://www.serena.com/index.php/en/products/applic ...

  10. Bootstrap弹出框(modal)垂直居中

    最近在做一个eit项目,由于此项目里面一些框架要遵循nttdata的一些规则,故用到了Bootstrap这个东东,第一次碰到这个东东,有很大抵触,觉得不好,但用起来我觉得和别的弹出框真没什么两样.废话 ...