博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
深搜入门
阅读量:6406 次
发布时间:2019-06-23

本文共 3627 字,大约阅读时间需要 12 分钟。

问题描述

Alas! A set of D (1 <= D <= 15) diseases (numbered 1..D) is running through the farm. Farmer John would like to milk as many of his N (1 <= N <= 1,000) cows as possible. If the milked cows carry more than K (1 <= K <= D) different diseases among them, then the milk will be too contaminated and will have to be discarded in its entirety. Please help determine the largest number of cows FJ can milk without having to discard the milk.

输入

* Line 1: Three space-separated integers: N, D, and K 
* 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. 

输出

* Line 1: M, the maximum number of cows which can be milked.

样例输入

6 3 201 11 21 32 2 12 2 1

样例输出

5

提示

OUTPUT DETAILS: 
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). 
题意:有n头牛 d中病毒 如果牛感染超过k中病毒就视为不健康  求最多有多少头牛是健康的  ( 给你一些集合n,让你从中取一些集合求并,并之后集合的元素个数不能超过k。问你最多取多少集合。)
#include 
#include
#include
#include
#include
#include
#include
using namespace std;const int oo = 0x3f3f3f3f;const int N = 200;const int M = 6000;typedef long long LL;struct da{ int k; int die[50]; bool operator <(const da &a)const { return k < a.k; }}cow[1111];int vis[1111], n, D, K, ans, dis[1111];void dfs(int x, int sum, int maxs){ if(sum > K) return ; ans = max(ans, maxs); // if(x == n+1 && ans < maxs) ans = maxs; if(x > n)return ; for(int i = x; i <= n; i++) { if(vis[i] == 0) { vis[i] = 1; int cnt = 0; for(int j = 1; j <= cow[i].k; j++) { if(dis[cow[i].die[j]] == 0) cnt++; dis[cow[i].die[j]]++; } // if(sum+cnt <= K && maxs+1 > ans) ans = maxs + 1; //sum += cnt; dfs(i+1, sum+cnt, maxs+1); vis[i] = 0; // sum -= cnt; for(int j = 1; j <= cow[i].k; j++) dis[cow[i].die[j]]--; } } dfs(x+1, sum, maxs);}int main(){ int i; while(~scanf("%d %d %d", &n, &D, &K)) { memset(vis, 0, sizeof(vis)); memset(dis, 0, sizeof(dis)); for(i = 1; i <= n; i++) { scanf("%d", &cow[i].k); for(int j = 1; j <= cow[i].k; j++) scanf("%d", &cow[i].die[j]); } ans = 0; sort(cow, cow+n); dfs(1, 0, 0); printf("%d\n", ans); } return 0;}
TLE!!!

TLE 了之后 借鉴了大神的想法换了种搜索的方式  很巧妙 我竟没想到(当选出的病毒数为K时,就遍历这N头牛,如果有牛所携带的病毒包含在K这些病毒里面  m++) 

#include 
#include
#include
#include
#include
#include
#include
using namespace std;const int oo = 0x3f3f3f3f;const int N = 200;const int M = 6000;typedef long long LL;struct da{ int k; int die[50]; bool operator <(const da &a)const { return k < a.k; }} cow[1111];int vis[1111], n, D, K, ans;void solve(){ int i, j, mini = 0; for(i = 1; i <= n; i++) { for(j = 1; j <= cow[i].k; j++) { if(!vis[cow[i].die[j]])/**< 如果这头牛所感染的病毒没有出现过 这头牛就不能选 */ break; } if(j == cow[i].k+1) mini++;/**< 这头牛所携带的病毒都出现过 选上这头牛 */ } ans = max(ans, mini);}void dfs(int x, int sum){ if(sum == K)solve(); if(x > D) return ;/**< 递归结束边界 */ vis[x] = 1;/**< 标记数组 标记这个病毒是否出现过 */ dfs(x+1, sum+1); vis[x] = 0; dfs(x+1, sum);}int main(){ int i; while(~scanf("%d %d %d", &n, &D, &K)) { memset(vis, 0, sizeof(vis)); for(i = 1; i <= n; i++) { scanf("%d", &cow[i].k); for(int j = 1; j <= cow[i].k; j++) scanf("%d", &cow[i].die[j]); } ans = 0; sort(cow, cow+n);/**< 貌似可以不排序 */ dfs(1, 0); printf("%d\n", ans); } return 0;}

  

转载于:https://www.cnblogs.com/PersistFaith/p/4819914.html

你可能感兴趣的文章
【概率论与数理统计】小结9-3 - 区间估计
查看>>
Golang性能调优入门
查看>>
sqlloader外部表
查看>>
golang笔记——数组与切片
查看>>
屏蔽可忽略的js脚本错误
查看>>
散文分享
查看>>
【Vue】vue.js常用指令
查看>>
NFS学习
查看>>
MySql常用命令总结
查看>>
又一年...
查看>>
文件上传框的美化+预览+ajax
查看>>
Linux VFS
查看>>
ext不能选中复制属性_如何实现Extjs的grid单元格只让选择(即可以复制单元格内容)但是不让修改?...
查看>>
python中print的作用*8、不能+8_在 Python 3.x 中语句 print(*[1,2,3]) 不能正确执行。 (1.0分)_学小易找答案...
查看>>
python 生成html代码_使用Python Markdown 生成 html
查看>>
axure如何导出原件_Axure 教程:轻松导出图标字体所有图标
查看>>
laravel input值必须不等于0_框架不提供,动手造一个:Laravel表单验证自定义用法...
查看>>
cad填充图案乱理石_太快了吧!原来大神是这样用CAD图案填充的
查看>>
activator.createinstance 需要垃圾回收么_在垃圾回收器中有哪几种判断是否需要被回收的方法...
查看>>
rocketmq 消息指定_RocketMQ入坑系列(一)角色介绍及基本使用
查看>>