hihocoder week 86 Spring Outing【思维】
2016-02-29 21:04
489 查看
P1 : Spring Outing
Time Limit:20000msCase Time Limit:1000ms
Memory Limit:256MB
Description
You class are planning for a spring outing. N people are voting for a destination out of K candidate places.The voting progress is below:
First the class vote for the first candidate place. If more than half of the class agreed on the place, the place is selected. The voting ends.
Otherwise they vote for the second candidate place. If more than half of the class agreed on the place, the place is selected. The voting ends.
Otherwise they vote for the third candidate place in the same way and go on.
If no place is selected at last there will be no spring outing and everybody stays at home.
Before the voting, the Chief Entertainment Officer did a survey, found out every one's preference which can be represented as a permutation of 0, 1, ... K. (0 is for staying at home.) For example, when K=3, preference
"1, 0, 2, 3" means that the first place is his first choice, staying at home is the second choice, the second place is the third choice and the third place is the last choice.
The Chief Entertainment Officer sends the survey results to the class. So everybody knows the others' preferences. Everybody wants his more prefered place to be selected. And they are very smart, they always choose
the optimal strategy in the voting progress to achieve his goal.
Can you predict which place will be selected?
Input
The first line contains two integers, N and K, the number of people in your class and the number of candidate places.The next N lines each contain a permutation of 0~K, representing someone's preference.
For 40% of the data, 1 <= N, K <= 10
For 100% of the data, 1 <= N, K <= 1000
Output
Output the selected place. Or "otaku" without quotes if no place is selected.
Hint
In the sample case, if the second peoson vote against the first place, no place would be selected finally because the first person must vote against the second place for his own interest. Considering staying athome is a worse choice than the first place, the second person's optimal strategy is voting for the first place. So the first place will be selected.
Sample Input
2 2 1 0 2 2 1 0
Sample Output
1
题意:有n个人对决定出去游玩,他们有m个备选的游玩地点(1..m)或者选择干脆宅在家(0)。每个人在自己心目中对这m+1(0..m)个地点有一个排名,他们会根据这个列表进行投票。对于m个地点,按1,2,3,...,m的顺序依次进行表决。当某一个方案票数超过一半时,则投票结束,选择这个地点作为结果。如果m个地点都没有超过半数,那就选择每个人都宅在家里。现在将每一个人心目中的排名公布出来,即每个人都知道所有人对于m+1个地点的排名。同时他们都非常聪明,会尽量让自己更喜欢的地点胜出,求问最后会选择哪一个地方作为游玩地点。
思路:考虑最后一轮投票,投反对票的人一定认为0比m好,投赞成票的一定认为m比0好。而对于个人而言,0和m好不好在于它们在自己心目中的排名。这样的话对于给定的信息,可以确定最后一轮投票的结果。继续向下推,会发现第i-1轮投票可以唯一确实第i轮投票结果。于是我们可以从第m轮结果开始向前推,发现有更靠前的地点满足就更新。
AC代码:
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <map> #include <string> using namespace std; typedef long long LL; const int MAXN = 1005; const int INF = 0x3f3f3f3f; int id[MAXN][MAXN]; int main() { int n, m; while(scanf("%d%d", &n, &m) != EOF) { for(int i = 1; i <= n; i++) { for(int j = 0; j <= m; j++) { int v; scanf("%d", &v); id[i][v] = j; } } int ans = 0; for(int i = m; i >= 1; i--) { int cnt = 0; for(int j = 1; j <= n; j++) if(id[j][ans] > id[j][i]) cnt++; if(cnt > n / 2) ans = i; } printf(ans ? "%d\n" : "otaku\n", ans); } return 0; }
相关文章推荐
- 112. Path Sum
- java中volatile关键字的含义
- Spring mvc整合Mybatis例子
- Java回调
- hbase问题
- 9. Palindrome Number
- java学习---操作符
- SpringMVC 文件上传配置,多文件上传,使用的MultipartFile
- JAVA学习笔记——JAVA中的IO流
- Spring 源码分析(三) —— AOP(一)AOP原理
- java中静态代码块的用法 static用法详解
- 119. Pascal's Triangle II
- GraphChi/graphchi-java程序配置
- mac下查找设置$JAVA_HOME路径
- 118. Pascal's Triangle
- 如何使用spring
- java String字符串
- 解决Eclipse建立Maven项目后无法建立src/main/java资源文件夹的办法
- 66. Plus One
- 26. Remove Duplicates from Sorted Array