浙江省赛 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?相关推荐
- 【2016浙江省赛:区间取模】E : Modulo Query | ZOJ - 3940
2016浙江省赛:E 题 Modulo Query [难度] 4.5/104.5/104.5/10 据说是卡银题?感觉有点难 [题意] F(i,X)={XmodA1i=1F(i−1,X)modAi2≤ ...
- 2021浙江省赛题解(A,C,F,G,J,L,M)
2021浙江省赛题解(A,C,F,G,J,L,M) A.League of Legends 题解 签到题 直接求和判断一下 注意会爆 i n t int int以及相等的情况. 代码 #include ...
- 2022浙江省赛、ICPC昆明区域赛 游·寄
前夜 周六打的浙江省赛.周日昆明区域赛,周五平常作息,早上和爸妈聊天 我说道 金华疫情情况还好,还能出校吃饭,没想到 噩耗马上就来了.金华突然有了几例阳性,其中有一位还是滴滴司机.线上教学的消息已发出 ...
- 【2019浙江省赛 - K 】Strings in the Pocket(马拉车,思维)
题干: BaoBao has just found two strings and in his left pocket, where indicates the -th character i ...
- The 13th Zhejiang Provincial Collegiate Contest(2016年浙江省赛)
前4道水题就不说了,其中我做了C题,1Y,小心仔细写代码并且提交之前得确认无误后提交才能减少出错率. 结果后面2题都由波神做掉,学长带我们飞~ 终榜 官方题解 ZOJ 3946 Highway ...
- The 12th Zhejiang Provincial Collegiate Programming Contest - I Earthstone Keeper浙江省赛
题目:http://acm.zju.edu.cn/onlinejudge/showContestProblem.do?problemId=5501 思路:DFS,用了递归就溢出,所以可能得用非递归的. ...
- 2016浙江省赛过山车记
省赛后一天就是期中考真刺激,可以体验连续滚粗的快感. 听说今天是鸟神的生日,于是凌晨造了个大新闻,强行给鸟神灌了一大口毒奶. 热身赛写模拟写到结束也没调出来,给下午滚粗奠定了坚实的基础. 正赛,打开题 ...
- ZOJ4100 浙江省赛16th Problem A
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=4100 比赛的时候封榜开始开这题,因为我的愚蠢没有开出来. 查询的最小值不用 ...
- 2017浙江省赛 B - Problem Preparation ZOJ - 3959
地址:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3959 题目: It's time to prepare the pr ...
- 【2019浙江省赛 - A】Vertices in the Pocket(权值线段树下二分,图,思维)
题干: DreamGrid has just found an undirected simple graph with vertices and no edges (that's to say, ...
最新文章
- [转]Emacs 系列教程
- 1.How Models work
- LeetCode 1191. K 次串联后最大子数组之和(前缀和+分类讨论)
- 【Drools一】值得打工人学习的规则引擎Drools
- scrapy 入门案例
- javaweb(02) JavaScript基础知识
- innodb 共享表空间 转 独立表空间 详细说明
- 矩阵分解之奇异值分解
- 针对云原生转型的6个关键数据策略
- 自己研发的系统给rtx发消息
- echarts循环图表
- Vs2010激活 系统延长期限
- 微信小程序打包超过2M 解决方法
- 哈利波特英文单词统计频率
- 在 Airbnb 使用机器学习预测房源的价格
- 考研数学要背诵的知识点
- 京东疯狂月薪36k程序员最新Java面试题(常见面试题及答案汇总)
- Python基础第一周
- SCM:新一代存储介质技术
- java is as,as-is是什么意思
热门文章
- 医咖会免费STATA教程学习笔记——无序多分类回归
- 基于Vue和SpringBoot的论文检测系统的设计与实现
- 聋校计算机教材教法培训Ppt,小学数学教材教法培训.ppt
- 分析CRM系统的十大功能
- 玩转RT-Thread系列教程(6)--移植STemwin
- 2020CVPR超分系列二Deep Unfolding Network for Image SR+Meta-Transfer Learning ZSSR+Res FeatureAggregation
- 计算机英语多层,大学专业英语:计算机英语2
- [PHP源码阅读]strtolower和strtoupper函数
- 设置WIN10资源管理器文件视图,使其所有列为 合适大小或固定长度
- MyBatis学习总结_13_Mybatis查询之resultMap和resultType区别