浙江省赛 C What Kind of Friends Are You?
思路:一开始考虑n个朋友可以有c种可能,根据回答的问题,如果是yes那么可以确定一些朋友的范围,将原本不在这个范围内的删除即可;如果是“no”,说明这些朋友都应该被删除,那么最后看第i个人候选的情况是不是只有一种,如果是直接输出名字,否则就是不确定或者不存在。下面介绍利用贡献优化,并且非常好写的方法。
AC代码
#include <cstdio> #include <cmath> #include <cctype> #include <algorithm> #include <cstring> #include <utility> #include <string> #include <iostream> #include <map> #include <set> #include <vector> #include <queue> #include <stack> using namespace std; #pragma comment(linker, "/STACK:1024000000,1024000000") #define eps 1e-10 #define inf 0x3f3f3f3f #define PI pair<int, int> typedef long long LL; const int maxn = 200 + 5; string name[maxn]; int vis[maxn][maxn]; int a[maxn][maxn]; map<string, int>ha; int getId(string p) { return ha[p]; } int main() { int T, n, q, c; scanf("%d", &T); while(T--) { ha.clear(); scanf("%d%d", &n, &q); scanf("%d", &c); for(int i = 0; i < c; ++i) { cin >> name[i]; ha[name[i]] = i; } vector<int>que[maxn]; memset(vis, 0, sizeof(vis)); for(int i = 0; i < q; ++i) { int m; scanf("%d", &m); string s; for(int j = 0; j < m; ++j) { cin >> s; que[i].push_back(getId(s)); } } for(int i = 0; i < n; ++i) { for(int j = 0; j < c; ++j) vis[i][j] = 1; for(int j = 0; j < q; ++j) { scanf("%d", &a[i][j]); if(a[i][j]) { for(int k = 0; k < c; ++k) if(vis[i][k]) { int f = 0; for(int h = 0; h < que[j].size(); ++h) { if(que[j][h] == k) { f = 1; break; } } if(!f) vis[i][k] = 0; } } else { for(int k = 0; k < que[j].size(); ++k) { vis[i][que[j][k]] = 0; } } } } for(int i = 0; i < n; ++i) { int cnt = 0; int ind; for(int j = 0; j < c; ++j) { if(vis[i][j]) { ++cnt; ind = j; } } if(cnt == 1) { cout << name[ind] << endl; } else printf("Let's go to the library!!\n"); } } return 0; }
更好的思路:每一个人被每一个问题赋予了一定的贡献,第i个问题的贡献是2^i(二进制)。
例如:
4 Serval Raccoon Alpaca Moose
1 Serval
1 Fennec
1 Serval
第一个问题对Serval Raccoon Alpaca Moose四人的贡献为2^0,
第二个问题对Serval贡献为2^1,下面的问题同理;“”
最后根据对问题的回答情况,确定第i(0 < i < n)人的贡献,查找人名中是否有唯一与之对应的,如果有输出人名,否则输出“Let's go to the library!!”,时间复杂度为O(n*c)。这种方法非常巧妙,降低了复杂度。
AC代码
#include <cstdio> #include <cmath> #include <cctype> #include <algorithm> #include <cstring> #include <utility> #include <string> #include <iostream> #include <map> #include <set> #include <vector> #include <queue> #include <stack> using namespace std; #pragma comment(linker, "/STACK:1024000000,1024000000") #define eps 1e-10 #define inf 0x3f3f3f3f #define PI pair<int, int> typedef long long LL; const int maxn = 200 + 5; string name[maxn]; map<string, int>ha; int w[30], g[maxn]; void init() { w[0] = 1; for(int i = 1; i <= 25; ++i) w[i] = w[i-1] * 2; } int main() { init(); int T, n, q, c; scanf("%d", &T); while(T--) { ha.clear(); scanf("%d%d", &n, &q); scanf("%d", &c); for(int i = 0; i < c; ++i) { cin >> name[i]; ha[name[i]] = i; g[i] = 0; } for(int i = 0; i < q; ++i) { string s; int m; scanf("%d", &m); while(m--) { cin >> s; g[ha[s]] += w[i]; } } for(int i = 0; i < n; ++i) { int sum = 0; for(int j = 0; j < q; ++j) { int f; scanf("%d", &f); if(f) sum += w[j]; } int cnt = 0, peo; for(int j = 0; j < c; ++j) { if(g[j] == sum) { ++cnt; peo = j; } } if(cnt == 1) cout << name[peo] << endl; else printf("Let's go to the library!!\n"); } } return 0; }
由于测试数据不够强,导致二种方法时间相差不大:强烈建议掌握第二种的思路。
如有不当之处欢迎指出!
浙江省赛 C What Kind of Friends Are You?的更多相关文章
- ZOJ 3879 Capture the Flag 15年浙江省赛K题
每年省赛必有的一道模拟题,描述都是非常的长,题目都是蛮好写的... sigh... 比赛的时候没有写出这道题目 :( 题意:首先输入4个数,n,q,p,c代表有n个队伍,q个服务器,每支队伍的初始分数 ...
- The 13th Zhejiang Provincial Collegiate Contest(2016年浙江省赛)
前4道水题就不说了,其中我做了C题,1Y,小心仔细写代码并且提交之前得确认无误后提交才能减少出错率. 结果后面2题都由波神做掉,学长带我们飞~ 终榜 官方题解 ZOJ 3946 Highway ...
- ZOJ 3872 Beauty of Array DP 15年浙江省赛D题
也是一道比赛时候没有写出来的题目,队友想到了解法不过最后匆匆忙忙没有 A 掉 What a pity... 题意:定义Beauty数是一个序列里所有不相同的数的和,求一个序列所有字序列的Beauty和 ...
- (2017浙江省赛E)Seven Segment Display
Seven Segment Display Time Limit: 2 Seconds Memory Limit: 65536 KB A seven segment display, or ...
- 浙江省赛之Singing Everywhere
题目:http://acm.zju.edu.cn/onlinejudge/showContestProblem.do?problemId=5996 方法: 在大佬的指导下完成. 寻找峰值,找到一共k个 ...
- 2019浙江省赛B zoj4101 Element Swapping(推公式)
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=6003 题意 \(数组a通过交换一对数字,得到了b数组,给出x=\sum^n_{ ...
- 2019浙江省赛K zoj4110 Strings in the Pocket(manachar)
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=6012 题意 给你两个串,可以翻转a串的一个区间,问有多少对l,r使得翻转后的a ...
- 140 - The 12th Zhejiang Provincial Collegiate Programming Contest(浙江省赛2015)
Ace of Aces Time Limit: 2 Seconds Memory Limit: 65536 KB There is a mysterious organization c ...
- 浙江省赛 ZOJ4029
Now Loading!!! Time Limit: Second Memory Limit: KB DreamGrid has integers . DreamGrid also has queri ...
随机推荐
- AI_深度学习概论
什么是是神经网络? 假如有6间房屋的数据集,已知房子的面积,单位是平方米或平方英尺,已知房子的价格.如果通过这6间房子的价格和房子的面积,预测房子的价格,首先要建立起一个数据模型 ,x轴为价格,y轴为 ...
- web技术发展历程--读《大型网站技术架构_核心原理与案例分析》
1 早期的web服务 2 CGI程序的出现.发展.凋零到MVC的兴起 CGI:通用网关接口技术. 随着CGI技术的出现,web服务端可以通过不同的用户请求产生动态页面内容. web服务器将请求数据交给 ...
- 如何用命令将本地项目上传到github
一.Git终端软件安装 1.下载windows上git终端,类似shell工具,下载地址:http://msysgit.github.io/ 2.安装方法,打开文件,一路点击Next即可 3.安装完成 ...
- nodejs爬虫笔记(一)---request与cheerio等模块的应用
目标:爬取慕课网里面一个教程的视频信息,并将其存入mysql数据库.以http://www.imooc.com/learn/857为例. 一.工具 1.安装nodejs:(操作系统环境:WiN 7 6 ...
- 【转】一些常用的Vi命令,可帮助脱离鼠标
使用Vi编写代码时,如果想脱离鼠标,需要使用一些命令快捷键,下面罗列了一些常用的并且容易记住的: 1. 命令模式下,移动光标或跳转 0到行首 ^到行首第一个非空字符 $到行尾非空字符 fx向后移动光标 ...
- 2. getline()和get()
1.面向行输入:getline() ---其实还可以接受第三个参数. getline()函数读取整行,调用该方法 使用cin.getline().该函数有两个参数, 第一个参数是是用来存储输入行的数组 ...
- 程序员之殇 —— (Are you afraid of me? Don't be.)灵感=神秘感
Are you afraid of me? (你们怕我吗?) Don't be.(不用怕) I am a programmer who just won't die.(我是不会死的程序员) 自从跟踪到 ...
- css层叠样式初学
一.css简介 1.层叠样式表:叠加效果,不同css对同一html修饰,冲突部分,优先级高作用,不冲突部分,共同作用 2.css作用 (1)修饰html (2)替代了标签自身的颜色,字号等属性,提高复 ...
- javascript 中 dom.getAttribute("value") 与dom.value的差异
dom 是一个 input type="text" 手动修改 input 的值, 使用 dom.getAttribute("value") 只能得到 html ...
- vue中多个input绑定enter按键事件
默认再每个input上加 keyup.enter 是可以绑定事件的 可是多个input一起绑定同一个事件就比较麻烦 所以就在vue dom里面遍历查找并绑定事件 let handleEnter = ( ...