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 [难理解]的更多相关文章

  1. PAT 1148 Werewolf - Simple Version

    1148 Werewolf - Simple Version (20 分)   Werewolf(狼人杀) is a game in which the players are partitioned ...

  2. PAT(A) 1148 Werewolf - Simple Version(Java)逻辑推理

    题目链接:1148 Werewolf - Simple Version (20 point(s)) Description Werewolf(狼人杀) is a game in which the p ...

  3. PAT A1148 Werewolf - Simple Version (20 分)——暴力遍历,负负得正

    Werewolf(狼人杀) is a game in which the players are partitioned into two parties: the werewolves and th ...

  4. 1148 Werewolf - Simple Version (20 分)

    Werewolf(狼人杀) is a game in which the players are partitioned into two parties: the werewolves and th ...

  5. 1148 Werewolf - Simple Version

    Werewolf(狼人杀) is a game in which the players are partitioned into two parties: the werewolves and th ...

  6. PAT_A1148#Werewolf - Simple Version

    Source: PAT 1148 Werewolf - Simple Version (20 分) Description: Werewolf(狼人杀) is a game in which the ...

  7. K8S核心概念之SVC(易混淆难理解知识点总结)

    本文将结合实际工作当中遇到的一些问题和情况来解析SVC的作用以及一些比较易混淆和难理解的概念,方便日后工作用到或者遗忘时可以直接在自己曾经学习总结的博客当中直接查找到. 首先应该清楚SVC的作用是什么 ...

  8. PAT 1056 Mice and Rice[难][不理解]

    1056 Mice and Rice(25 分) Mice and Rice is the name of a programming contest in which each programmer ...

  9. 认为C/C++很难理解、找工作面试笔试,快看看这本书!

    假设你是C/C++谁刚开始学习,看这本书.因为也许你读其他的书还不如不看.一定要选择一本好书. 假设你正在准备工作,请认真看这本书,由于这本书会教会你工作中必备的知识,相信你即将面临的语法类题目不会超 ...

随机推荐

  1. 如何远程备份sql server数据库

      方法一(不使用SQLDMO): /// ///备份方法 /// SqlConnection conn = new SqlConnection("Server=.;Database=mas ...

  2. 在Asp.net中为图像加入水印信息

    using System.Drawing; using System.IO; using System.Drawing.Imaging; private void AddTextToImg(strin ...

  3. 超简单CSS3实现圆角、阴影、透明效果

    CSS实现圆角,阴影,透明的方法很多,传统的方法都比较复杂,用CSS3就方便很多了,虽然现在各浏览器对CSS3的支持还不是很好,但不久的将来CSS3就会普及. 1.圆角 CSS3实现圆角有两种方法. ...

  4. C++ 构造中调用构造

    //构造中调用构造 #define _CRT_SECURE_NO_WARNINGS #include<iostream> using namespace std; class Point{ ...

  5. (转)内核container_of(ptr,type,member) 解析

     container_of(ptr,type,member) 用于在已知结构体里面成员member和该成员指针ptr(就是地址)和结构体类型type, 返回该成员所在的结构体的指针(就是地址), 例如 ...

  6. __name__

    __name__ 是 python 的一个内置变量,它的值等于 '__main__' ,如下: [root@localhost ~]$ cat talk.py #!/usr/bin/env pytho ...

  7. Linux产生随机数的几种常见方法

    方法1: [root@localhost ~]# echo $RANDOM 方法二 [root@localhost ~]# openssl rand -base64 7X6HMer5hhY= 方法三 ...

  8. 关东升的iOS实战系列图书 《iOS实战:入门与提高卷(Swift版)》已经上市

             承蒙广大读者的厚爱我的 <iOS实战:入门与提高卷(Swift版)>京东上市了,欢迎广大读者提出宝贵意见.http://item.jd.com/11766718.html ...

  9. 《从零开始学Swift》学习笔记(Day 68)——Cocoa Touch设计模式及应用之响应者链与触摸事件

    原创文章,欢迎转载.转载请注明:关东升的博客 应用与用户进行交互,依赖于各种各样的事件.事件响应者对象是可以响应事件并对其进行处理的对象,响应者链是由一系列链接在一起的响应者组成的.响应者链在事件处理 ...

  10. Spring 之注解事务 @Transactional(转载)

    Spring在TransactionDefinition接口中规定了7种类型的事务传播行为, 它们规定了事务方法和事务方法发生嵌套调用时事务如何进行传播: 事务传播行为类型 事务传播行为类型 说明 P ...