HDU 5491 The Next(表示我的方法比较暴力,但需要考虑的东西比较少)——2015 ACM/ICPC Asia Regional Hefei Online
2015-09-27 18:17
621 查看
The Next
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)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
Source
2015 ACM/ICPC Asia Regional Hefei Online
/*********************************************************************/
题意:给你一个数D,它的二进制表示中'1'的个数为L,满足S1≤L≤S2,问比D大的同样满足'1'的个数在S1~S2之间的最小的数是多少
解题思路:毕竟比较菜鸟,这个题目虽然水,但刚开始并没有想到什么好的方法,也是不断举举例子,才发现了一种做法
对于任意给定的一个D,比如11,它的二进制表示为(1011)2,我们暂且在最高位前添一个0(因为对于1111这样的二进制数,要比它大,就得在最高位前添一位),此时,D为(01011)2,我们需要做的事情很简单:
因为要比原来的数大,那至少需要把一个'0'改成'1',所以我们只需要遍历所有的'0',将该'0'后面的数清空后,从低位开始向高位添'1',直到'1'的个数满足在S1~S2之内,最后取最小值即可
#pragma comment(linker, "/STACK:1024000000,1024000000") #include<stdio.h> #include<string.h> #include<stdlib.h> #include<queue> #include<stack> #include<math.h> #include<vector> #include<map> #include<set> #include<stdlib.h> #include<cmath> #include<string> #include<algorithm> #include<iostream> #define exp 1e-10 using namespace std; const int N = 40; const __int64 inf = 10000000000000; const int mod = 2009; int s ,a ; int main() { int t,i,j,d,s1,s2,k=1,p,c,cc,tt; __int64 ans,Min; scanf("%d",&t); while(t--) { c=p=0;Min=inf; scanf("%d%d%d",&d,&s1,&s2); while(d) { if(d%2) c++; s[p++]=d%2; d/=2; } s[p++]=0; for(tt=i=0;i<p;i++) { if(!s[i]) { for(cc=j=0;j<i&&c-tt+1+cc<s1;j++) a[j]=1,cc++; for(;j<i;j++) a[j]=0; //printf("*%d*\n",cc+1+c-tt); if(cc+1+c-tt<=s2) a[i]=1; else { a[i]=0; continue; } /*for(j=p-1;j>=0;j--) printf("%d ",s[j]); puts("##"); for(j=i;j>=0;j--) printf("%d ",a[j]); puts("@@");*/ for(ans=0,j=p-1;j>i;j--) ans=ans*2+s[j]; for(;j>=0;j--) ans=ans*2+a[j]; //printf("%I64d***\n",ans); Min=min(ans,Min); } else tt++; } printf("Case #%d: %I64d\n",k++,Min); } return 0; }菜鸟成长记
相关文章推荐
- Caffe源码中各种依赖库的作用及简单使用
- Caffe源码中各种依赖库的作用及简单使用
- 20150927js学习回顾
- 使用CSS达到阴阳八卦图等图形
- HDU - 5491 The Next 2015 ACM/ICPC Asia Regional Hefei Online
- JavaScript高级程序设计之面向对象的程序设计之继承之原型链 第6.3.1讲笔记
- HDU 5486 Difference of Clustering 图论
- JSP的基础语法
- HTML5新增的元素和废除的元素
- HTML初步学习2
- 深入javascript-继承机制
- Javascript--函数技巧
- jQuery 关于时间的相互转化
- jQuery 匹配正整数
- CSS命名规范
- [LeetCode][JavaScript]House Robber II
- 前端工程师所不知道的<meta>标签用法
- css3 新增属性
- HTML5的语法的改变
- 浏览器兼容、CSS3 选择器