PAT 1148 Werewolf - Simple Version [难理解]
1148 Werewolf - Simple Version (20 分)
Werewolf(狼人杀) is a game in which the players are partitioned into two parties: the werewolves and the human beings. Suppose that in a game,
- player #1 said: "Player #2 is a werewolf.";
- player #2 said: "Player #3 is a human.";
- player #3 said: "Player #4 is a werewolf.";
- player #4 said: "Player #5 is a human."; and
- player #5 said: "Player #4 is a human.".
Given that there were 2 werewolves among them, at least one but not all the werewolves were lying, and there were exactly 2 liars. Can you point out the werewolves?
Now you are asked to solve a harder version of this problem: given that there were N players, with 2 werewolves among them, at least one but not all the werewolves were lying, and there were exactly 2 liars. You are supposed to point out the werewolves.
Input Specification:
Each input file contains one test case. For each case, the first line gives a positive integer N (5≤N≤100). Then N lines follow and the i-th line gives the statement of the i-th player (1≤i≤N), which is represented by the index of the player with a positive sign for a human and a negative sign for a werewolf.
Output Specification:
If a solution exists, print in a line in ascending order the indices of the two werewolves. The numbers must be separated by exactly one space with no extra spaces at the beginning or the end of the line. If there are more than one solution, you must output the smallest solution sequence -- that is, for two sequences A=a[1],...,a[M] and B=b[1],...,b[M], if there exists 0≤k<M such that a[i]=b[i] (i≤k) and a[k+1]<b[k+1], then A is said to be smaller than B. In case there is no solution, simply print No Solution
.
Sample Input 1:
5
-2
+3
-4
+5
+4
Sample Output 1:
1 4
Sample Input 2:
6
+6
+3
+1
-5
-2
+4
Sample Output 2 (the solution is not unique):
1 5
Sample Input 3:
5
-2
-3
-4
-5
-1
Sample Output 3:
No Solution
题目大意:狼人杀里有狼人和村民,假设在一群人中一共有两个狼人,并且至少有一个狼人但并不是所有的狼人都说慌,而且恰好有两个说谎的人,那么你需要找出狼人。如果解不唯一,输出序号小的一组。
//当时考试的时候看着道题目,完全不理解,看了20分钟就放弃了,也是通过率最低的一道题目
//看柳神的题解,说是水题,我奔溃了。
代码来自:https://www.liuchuo.net/archives/6494
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> v(n+);
for (int i = ; i <= n; i++) cin >> v[i]; for (int i = ; i <= n; i++) {
for (int j = i + ; j <= n; j++) {
vector<int> lie, a(n + , );//都赋值为1.
a[i] = a[j] = -;//如果这两者是狼人。
for (int k = ; k <= n; k++)
if (v[k] * a[abs(v[k])] < ) lie.push_back(k);
//<0,则表示K在说谎。
if (lie.size() == && a[lie[]] + a[lie[]] == ) {
cout << i << " " << j;//两者都不是狼人。
return ;//直接返回,就是最小的标号
}
}
}
cout << "No Solution";
return ;
}
//这个判断<0,简直不要太厉害了。
1.v[i]表示i说v[i]是什么;
2.使用符号来判断是否说谎;
3.a[abs(v[k])]的符号用来判断当前假定条件下(a[i]和a[j]是狼人),v[k]是当前第k个人说v[k]是村民还是狼人,符号来表示,如果两者不一致,那么在此种假设下,就是说谎;
4.对于说谎的,如果长度正好为2,并且一个是狼人一个是村民,也就是二者的和为0,正负抵消。
PAT 1148 Werewolf - Simple Version [难理解]的更多相关文章
- PAT 1148 Werewolf - Simple Version
1148 Werewolf - Simple Version (20 分) Werewolf(狼人杀) is a game in which the players are partitioned ...
- PAT(A) 1148 Werewolf - Simple Version(Java)逻辑推理
题目链接:1148 Werewolf - Simple Version (20 point(s)) Description Werewolf(狼人杀) is a game in which the p ...
- PAT A1148 Werewolf - Simple Version (20 分)——暴力遍历,负负得正
Werewolf(狼人杀) is a game in which the players are partitioned into two parties: the werewolves and th ...
- 1148 Werewolf - Simple Version (20 分)
Werewolf(狼人杀) is a game in which the players are partitioned into two parties: the werewolves and th ...
- 1148 Werewolf - Simple Version
Werewolf(狼人杀) is a game in which the players are partitioned into two parties: the werewolves and th ...
- PAT_A1148#Werewolf - Simple Version
Source: PAT 1148 Werewolf - Simple Version (20 分) Description: Werewolf(狼人杀) is a game in which the ...
- K8S核心概念之SVC(易混淆难理解知识点总结)
本文将结合实际工作当中遇到的一些问题和情况来解析SVC的作用以及一些比较易混淆和难理解的概念,方便日后工作用到或者遗忘时可以直接在自己曾经学习总结的博客当中直接查找到. 首先应该清楚SVC的作用是什么 ...
- PAT 1056 Mice and Rice[难][不理解]
1056 Mice and Rice(25 分) Mice and Rice is the name of a programming contest in which each programmer ...
- 认为C/C++很难理解、找工作面试笔试,快看看这本书!
假设你是C/C++谁刚开始学习,看这本书.因为也许你读其他的书还不如不看.一定要选择一本好书. 假设你正在准备工作,请认真看这本书,由于这本书会教会你工作中必备的知识,相信你即将面临的语法类题目不会超 ...
随机推荐
- C++ c++与C语言的区别(空结构体)
//区别⑨:空结构体声明(C++版本) #include<iostream> using namespace std; struct A{}; class B{}; void main() ...
- request Dispatch
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletExcepti ...
- ios开发之--UIWebView全属性
最近的项目当中需要用到html和ios的交互,所以就凑空整理一下,所有webView相关的方法和属性,如有不对的地方,请大家不吝指教! 代码如下: 1,创建webview并设置代理 UIWebView ...
- iOS开发之--获取验证码倒计时及闪烁问题解决方案
大家在做验证码的时候一般都会用到倒计时,基本上大家实现的方式都差不多,先贴出一些代码来.. -(void)startTime{ __block ; //倒计时时间 dispatch_queue_t q ...
- Python 正则表达式贪婪模式
贪婪模式也就是我们使用 .* 匹配任意字符时会尽可能长地向后匹配,如果我们想阻止这种贪婪模式,需要加个问号,尽可能少地匹配,如下例子: In []: import re In []: html = ' ...
- Vim相关优化和配置
升级pythonwget https://www.python.org/ftp/python/3.6.5/Python-3.6.5.tgztar -xvf Python-3.6.5.tgzcd Pyt ...
- 第4章 Vim编辑器与Shell命令脚本
章节简述: 本章节将教给您如何使用Vim编辑器来编写文档.配置主机名称.网卡参数以及yum仓库 ,熟练使用各个模式和命令快捷键. 我们可以通过Vim编辑器将Linux命令放入合适的逻辑测试语句(if. ...
- 模拟window桌面实现
正在开发中的游戏有个全屏功能--可以在window桌面背景上运行,就像一些视频播放器在桌面背景上播放一样的,花了个上午整了个Demo放出来留个纪念. 实现功能:显示图标,双击图标执行相应的程序,右击图 ...
- webpack配置(二)
在配置webpack json loader的时候报错,如下: 解决方案: 首先,json文件中不能有注释 其次: 这里webpack.consig.js里面,modul下的loaders的loade ...
- 【BZOJ3881】[Coci2015]Divljak fail树+树链的并
[BZOJ3881][Coci2015]Divljak Description Alice有n个字符串S_1,S_2...S_n,Bob有一个字符串集合T,一开始集合是空的. 接下来会发生q个操作,操 ...