题目链接:

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. JS Nice – JavaScript 代码美化和格式化工具

    JS Nice 是一款让经过混淆处理的 JavaScript 代码可读更好的工具.它使用一种新型的用于 JavaScript 代码美化的去混淆和去压缩引擎.JSNice 采用先进的机器学习和程序分析技 ...

  2. iframe父页面获取子页面的高度

    最近做项目中用到了iframe,子页面更改父页面的高度,经过九九八十一难,找到了解决的办法. $(window).load(function() {  var h=$(document).height ...

  3. 利用JSONP实现跨域请求

    前言:有时候一忙起来就没了时间观念,原来我已经有十多天没写博客了.一直想做跨域方面的尝试,无奈最近准备校招没时间动动手.今天就先讲讲JSONP吧,昨晚还在研究QQ空间日志里面网络图片的问题呢,我发现日 ...

  4. android 最简单的自定义圆点view

    首先创建一个选择器,用来判断圆点状态,可以根本自己的需求改 <selector xmlns:android="http://schemas.android.com/apk/res/an ...

  5. 深入.net(多态一)

    代码优化技术: 当您在 编写一个类时,如果您发现你需要编写的“属性”和“方法”曾经在已有的类中实现,则,您可以将其共用的“属性”和“方法”剪切到一个新的“类”中,然后,让两个类共同继承这个“新类”.( ...

  6. GHOST WIN7系统64位经典优化版 V2016年

    来自系统妈:http://www.xitongma.com 深度技术GHOST win7系统32,64位经典优化版 V2016年3月 系统概述 深度技术ghost win7系统64位经典优化版适用于笔 ...

  7. windows svn 上传后 自动部署 到web目录下

    第一步 把web目录设置为工作目录 "D:\Program Files (x86)\VisualSVN Server\bin\svn.exe" upgrade "D:\y ...

  8. Hibernate 事务管理

    一. 事务包含四个基本特性:简称ACID: 1. Atomic(原子性):全部成功或全部失败: 2. Consistency(一致性):只有合法数据才能被写入,不合法则回滚到最初状态: 3. Isol ...

  9. XMLA连接器--免费但不开源通过ODBO、XMLA

    XMLA 连接器(驱动) :来自ARQUERY   http://jaist.dl.sourceforge.net/project/xmlaconnect/XMLA_Provider_v1.0.0.1 ...

  10. Symantec Backup Exec备份作业服务器盘符变更

    Symantec Backup Exec的备份作业中,如果某个服务器的磁盘更改了盘符,如果不修改备份作业里面的相关配置,就会出现类似下面的错误信息,如下截图所示 因为这台服务器上我们将原先的G盘的盘符 ...