[搜索] hdu 4016 Magic Bitwise And Operation
主题链接:
http://acm.hdu.edu.cn/showproblem.php?pid=4016
Magic Bitwise And Operation
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Others)
Total Submission(s): 1315 Accepted Submission(s): 504
For example, there are three integers 5, 6 and 7. You are asked to pick two of them. Your possible strategy is (5, 6), (5, 7) or (6, 7). The values when do bitwise and all the picked numbers together are as follows:
5 and 6 = 4
5 and 7 = 5
6 and 7 = 6
The smallest one is 4.
For each test case, there are two integers in the first line: n and k, denoting the number of given integers and the number of integers you are asked to pick out. n <= 40
The second line contains the n integers. You may assume that all integers are small than 2^60.
Notes: There are about one thousand randomly generated test cases. Fortunately 90% of them are relatively small.
3
3 2
5 6 7
8 2
238 153 223 247 111 252 253 247
40 10
1143632830316675007 558164877202423550 1152356080752164603 1143911006781551605 1132655005501751263
1152919305583327167 1141662230660382702 862439259920596463 1151777428397603327 1008771132016295871
855666336963428351 1151795583225167807 1152634943314572791 1071856693060561407 1132650872803426303
1124211056982081471 1152917106425982911 1152815392070041535 1080863910568853481 288230371856350975
1080720560532488126 864686455262281727 576460673919991167 574191342855241589 1152233760050118651
1152921504605798263 1152912708241186815 1079738008506187487 1075796261476483027 1080854478820730879
1152885219917823999 1151725162940854259 1147529498501577715 571956602920235519 1134545630643616248
1152921218991521790 1152921496000052703 1142788250826440703 1151654831778151421 1152780747522637695
Case #1: 4
Case #2: 9
Case #3: 36028797086245424
pid=4012" target="_blank" style="color:rgb(26,92,200); text-decoration:none">4012
4013 4014 4015题目意思:
在n个数中找k个,使得这k个数相与的值最小。
解题思路:
搜索+剪枝
剪枝一:假设当前值和最后全部的值想与都小于当前求得的值的话,直接返回。由于与运算是越来越小的。
剪枝二:从小到大排序。搜索的顺序对得到最优值的时间有非常大影响。
代码:
//#include<CSpreadSheet.h> #include<iostream>
#include<cmath>
#include<cstdio>
#include<sstream>
#include<cstdlib>
#include<string>
#include<string.h>
#include<cstring>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<ctime>
#include<bitset>
#include<cmath>
#define eps 1e-6
#define INF 0x3f3f3f3f
#define PI acos(-1.0)
#define ll __int64
#define LL long long
#define lson l,m,(rt<<1)
#define rson m+1,r,(rt<<1)|1
#define M 1000000007
//#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std; #define Maxn 45
int n,k;
ll sa[Maxn],la[Maxn];
ll ans; void dfs(int cur,int hav,ll now)
{
if(hav==k)
{
if(ans==-1)
ans=now;
else if(now<ans)
ans=now;
return ;
}
if(cur>n)
return ;
if(ans!=-1&&(now&la[cur])>=ans)
return ;
dfs(cur+1,hav+1,now==-1? sa[cur]:(now&sa[cur]));
dfs(cur+1,hav,now);
} int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int t,cnt=0;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&k);
for(int i=1;i<=n;i++)
scanf("%I64d",&sa[i]);
sort(sa+1,sa+n+1);
la[n]=sa[n];
for(int i=n-1;i>=1;i--)
la[i]=la[i+1]&sa[i];
ans=-1;
dfs(1,0,-1);
printf("Case #%d: %I64d\n",++cnt,ans);
} return 0;
}
版权声明:本文博客原创文章,博客,未经同意,不得转载。
[搜索] hdu 4016 Magic Bitwise And Operation的更多相关文章
- HDU 1153 magic bitstrings(读题+)
hdu 1153 magic bitstrings 题目大意 一个质数p,现在让你求一个p-1长度的“01魔法串”.关于这个魔法串是这么定义的: 我们现在把这个串经过一段处理变成一个长宽均为p ...
- hdu 4605 Magic Ball Game
http://acm.hdu.edu.cn/showproblem.php?pid=4605 可以离线求解 把所以可能出现的 magic ball 放在一个数组里(去重),从小到大排列 先不考虑特殊 ...
- HDU 2274 Magic WisKey
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2274 Magic WisKey Time Limit: 2000/1000 MS (Java/Othe ...
- hdu A Magic Lamp
http://acm.hdu.edu.cn/showproblem.php?pid=3183 A Magic Lamp Time Limit: 2000/1000 MS (Java/Others) ...
- HDU 4605 Magic Ball Game(可持续化线段树,树状数组,离散化)
Magic Ball Game Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) ...
- HDU 4602 Magic Ball Game(离线处理,树状数组,dfs)
Magic Ball Game Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) ...
- 【树形动规】HDU 5834 Magic boy Bi Luo with his excited tree
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5834 题目大意: 一棵N个点的有根树,每个节点有价值ci,每条树边有费用di,节点的值只能取一次,边 ...
- HDU 4605 Magic Ball Game (在线主席树|| 离线 线段树)
转载请注明出处,谢谢http://blog.csdn.net/ACM_cxlove?viewmode=contents by---cxlove 题意:给出一棵二叉树,每个结点孩子数目为0或者2. ...
- HDU 4323 Magic Number(编辑距离DP)
http://acm.hdu.edu.cn/showproblem.php?pid=4323 题意: 给出n个串和m次询问,每个询问给出一个串和改变次数上限,在不超过这个上限的情况下,n个串中有多少个 ...
随机推荐
- WPF对于xml的简单操作(上)
private void button1_Click(object sender, RoutedEventArgs e) { XmlTextWriter writer = new XmlTextWri ...
- gc overhead limit exceeded eclipse错误解决方式
在Eclipse打包的时候报错:gc overhead limit exceeded eclipse 原因是Eclipse默认配置内存太小须要更改安装Eclipse目录下的eclipse.ini文件. ...
- Java Map 迭代
Map迭代 有两种 道路 遍历 Map该方法: 1 Set<K> KeySet(): 获取全部的键,得到set集合,迭代, 通过get( key)获取值! 2 Se ...
- fedora linux平台下搭建lighttpd+php+sqlite
(一)lighttpd 1. 安装 yum install lighttpd 安装完毕后,系统中应该多了一个用户lighttpd和组lighttpd.这个用户,默认是不同意登陆的. 我们改动/etc/ ...
- springmvc+mongodb+maven 项目测试代码
你看我有一篇文章配置,或许还会有.mongodb性能测试结果.一个"快"字 源代码包,请留下邮箱 代码结构图 watermark/2/text/aHR0cDovL2Jsb2cuY3 ...
- Json的反序列化 .net Newtonsoft.Json
项目中有个.json文件. { "instances": [ { "name": "baidu", "url": &qu ...
- Threejs 它可以在建立其内部房间效果可见
Threejs 中建立可看到其内部的房间效果 太阳火神的漂亮人生 (http://blog.csdn.net/opengl_es) 本文遵循"署名-非商业用途-保持一致"创作公用协 ...
- HDU 2815 Mod Tree 离散对数 扩张Baby Step Giant Step算法
联系:http://acm.hdu.edu.cn/showproblem.php?pid=2815 意甲冠军: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQ ...
- 【Java基金会】Java整理面试问题和评论(一)
1. ArrayList,Vector, LinkedList 存储性能及特点 ArrayList 和 Vector 都是使用数组方式存储数据,此数组元素数大于实际存储的数据以便添加和插入元素,它们都 ...
- 创建Material Design风格Android应用--自定义阴影和裁剪视图
之前已经写过通过应用主题和使用ListView, CardView,应用Material Design样式,同一时候都都能够通过support library向下兼容.今天要写的阴影和视图裁剪.无法向 ...