BZOJ 1688: Disease Manangement (子集枚举)
Disease Manangement
Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u
Description
Input
* Lines 2..N+1: Line i+1 describes the diseases of cow i with a list of 1 or more space-separated integers. The first integer, d_i, is the count of cow i's diseases; the next d_i integers enumerate the actual diseases. Of course, the list is empty if d_i is 0.
Output
Sample Input
6 3 2
0
1 1
1 2
1 3
2 2 1
2 2 1
Sample Output
5
Hint
If FJ milks cows 1, 2, 3, 5, and 6, then the milk will have only two diseases (#1 and #2), which is no greater than K (2).
6 3 2 N,D,k
0 第一列是奶牛携带的疾病个数,后面表示疾病的种类
1 1
1 2
1 3
2 2 1
2 2 1 携带两种疾病,种类为1,2两类
x&=(x-1)表示将x转化为2进制,最低的为1的位变成0,看含有的1的个数。
这是“位运算”中的一种很经典的用法,“&”是“与”的意思。它具体点的意思就是把x的二进制表示数最右边的一个1变成0 例如:
e1:
x = 01001000
x-1 = 01000111
x&(x-1)= 01000000
e2:
x = 01001001
x-1 = 01001000
x&(x-1)= 01001000
可见只有前后x与x-1中的两个运算数都是 1 的时候结果才是1.
同理“|”运算也是两个运算数有一个为1就是1.
具体解释在代码中 AC代码:
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int n,d,k;
int N[+];
bool judge(int x)
{
int c=;
while(x)
{
c++; // 将x转化为2进制,看含有的1的个数。
x&=(x-); //将最低的为1的位变成0
}
if(c<=k) //限制个数
return ;
else
return ;
}
int main()
{
int s,t,total=;
scanf("%d%d%d",&n,&d,&k);
for(int i=; i<n; i++)
{
cin>>s;
for(int j=; j<s; j++)
{
cin>>t;
N[i]|=<<(t-); //1<<t-1(1的二进制数整体向左移t-1位)
//一起把二进制数的位数对应着来看,这两个数在这一位上有1的结果就是1,否则是0 }
}
for(int i=; i<(<<d); i++) //i<(1<<d)是当i不小于(1左移d位的数)时终止循环,枚举各子集对应的编码0,1,2,..2^d-1
{
if(judge(i))
{
int f=;
for(int j=; j<n; j++)
{
if((N[j]|i)==i) f++; //对应N[j]与i的并集与i相等,说明N[j]是它的子集
}
if(f>total)
total=f;
}
}
cout<<total<<endl;
return ;
}
BZOJ 1688: Disease Manangement (子集枚举)的更多相关文章
- 1688: [Usaco2005 Open]Disease Manangement 疾病管理( 枚举 )
我一开始写了个状压dp..然后没有滚动就MLE了... 其实这道题直接暴力就行了... 2^15枚举每个状态, 然后检查每头牛是否能被选中, 这样是O( 2^15*1000 ), 也是和dp一样的时间 ...
- 1688: [Usaco2005 Open]Disease Manangement 疾病管理
1688: [Usaco2005 Open]Disease Manangement 疾病管理 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 413 So ...
- 【BZOJ1688】[Usaco2005 Open]Disease Manangement 疾病管理 状压DP
[BZOJ1688][Usaco2005 Open]Disease Manangement 疾病管理 Description Alas! A set of D (1 <= D <= 15) ...
- 【最小生成树+子集枚举】Uva1151 Buy or Build
Description 平面上有n个点(1<=N<=1000),你的任务是让所有n个点连通,为此,你可以新建一些边,费用等于两个端点的欧几里得距离的平方. 另外还有q(0<=q< ...
- UVA11825 黑客的攻击 Hackers' Crackdown 状压DP,二进制,子集枚举
题目链接Click Here [题目描述] 假如你是一个黑客,侵入了一个有着\(n\)台计算机(编号为\(1.2.3....n\))的网络.一共有\(n\)种服务,每台计算机都运行着所有服务.对于每台 ...
- BZOJ1688 Disease Manangement 疾病管理
Disease Manangement 疾病管理 Description Alas! A set of D (1 <= D <= 15) diseases (numbered 1..D ...
- BZOJ 1688: [Usaco2005 Open]Disease Manangement 疾病管理 状压DP + 二进制 + 骚操作
#include <bits/stdc++.h> #define setIO(s) freopen(s".in","r",stdin) #defin ...
- BZOJ 1688: [Usaco2005 Open]Disease Manangement 疾病管理
Description Alas! A set of D (1 <= D <= 15) diseases (numbered 1..D) is running through the fa ...
- 【BZOJ】1688: [Usaco2005 Open]Disease Manangement 疾病管理(状压dp)
http://www.lydsy.com/JudgeOnline/problem.php?id=1688 很水的状压.. 提交了很多次优化的,但是还是100msT_T #include <cst ...
随机推荐
- maven上传自定义jar到本地仓库
mvn install:install-file -Dfile=D:/baidu/ueditor-1.1.1.jar -DgroupId=com.baidu.ueditor -Dartifact ...
- 【Cocos2d-X开发学习笔记】第27期:游戏背景之贴图地图类(CCTileMapAtlas)的使用
本系列学习教程使用的是cocos2d-x-2.1.4(最新版为3.0alpha0-pre) ,PC开发环境Windows7,C++开发环境VS2010 一.贴图地图类CCTileMapAtlas 除了 ...
- System.Threading.Timer 使用
//定义计时器执行完成后的回调函数 TimerCallback timecallback = new TimerCallback(WriteMsg); //定义计时器 System.Threading ...
- NGUI对象跟随鼠标拖拽移动
public Camera WNGUICamera; Vector3 _WoldPosition;//指针的初始位置 // Vector3 _WoldAng; Vector3 WscreenSpace ...
- 阿里云服务器[教程3]一键安装php+mysql+ftp+nginx环境
直接看地址 http://help.aliyun.com/manual?spm=0.0.0.0.F5PPZs&helpId=129
- [CSS3] Text ellipsis
Link: http://www.w3schools.com/cssref/tryit.asp?filename=trycss3_text-overflow div{ white-space: now ...
- Android Touch系统简介(二):实例详解onInterceptTouchEvent与onTouchEvent的调用过程
上一篇文章主要讲述了Android的TouchEvent的分发过程,其中有两个重要的函数:onInterceptTouchEvent和onTouchEvent,这两个函数可被重装以完成特定的逻辑.on ...
- php 自定义求数组差集,效率比自带的array_diff函数还要快(转)
<?phpfunction array_different($array_1, $array_2) { $array_2 = array_flip($array_2); //将数组键值调换 fo ...
- C++中模板类使用友元模板函数
在类模板中可以出现三种友元声明:(1)普通非模板类或函数的友元声明,将友元关系授予明确指定的类或函数.(2)类模板或函数模板的友元声明,授予对友元所有实例的访问权.(3)只授予对类模板或函数模板的特定 ...
- select poll epoll三者之间的比较
一.概述 说到Linux下的IO复用,系统提供了三个系统调用,分别是select poll epoll.那么这三者之间有什么不同呢,什么时候使用三个之间的其中一个呢? 下面,我将从系统调用原型来分析其 ...