#1154 :Spring Outing
来源:互联网 发布:爱奇艺独播的网络剧 编辑:程序博客网 时间:2024/05/22 02:00
- 样例输入
2 21 0 22 1 0
- 样例输出
1
描述
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?
输入
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 the selected place. Or "otaku" without quotes if no place is selected.
样例提示
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 at home 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.
参考了该链接思路 http://www.cnblogs.com/Patt/p/4678756.html
用 java 重写了一下
import java.io.BufferedReader;import java.io.FileInputStream;import java.io.IOException;import java.io.InputStreamReader;import java.util.HashMap;import java.util.Map;/** * * @author *逆推,最后一个城市投票时,只有两种可能性,选最后一个还是在家,只要这个城市的优先级比 在家高,那么肯定会投这个城市 *这样最后一个城市的投票结果就预先知道了R(k), 倒数第二个城市投票的时候,只有 该城市和 R(k)两种可能,所以倒数第二个城 *市的结果也知道了,依次类推 */public class Main {public static void main(String[] args) throws IOException {BufferedReader bf = new BufferedReader(new InputStreamReader(new FileInputStream("F:/in.txt")));String[] num = bf.readLine().split(" ");int N = Integer.valueOf(num[0]);int K = Integer.valueOf(num[1]);// place and weightMap<Integer, Integer>[] pref = new HashMap[N];for(int i = 0; i < N; i++){pref[i] = new HashMap<Integer, Integer>();}for(int i = 0; i < N; i++){String[] place = bf.readLine().split(" ");for(int j = 0; j <= K; j++){pref[i].put(Integer.valueOf(place[j]), K - j);}}int preR = 0;//倒推for(int pla = K; pla > 0; pla--){//N 个人投票int voteNum = 0;for(int people = 0; people < N; people++){if(pref[people].get(pla) > pref[people].get(preR)){voteNum++;}}if(voteNum >= N / 2 + 1){preR = pla;}}if(preR == 0){System.out.println("otaku");}else{System.out.println(preR);}}}
- #1154 :Spring Outing
- Spring Outing
- Spring Outing
- Spring Outing 解题报告
- 【ACM】Spring Outing
- HDU 1707 Spring-outing Decision
- 微软笔试题《Spring Outing》
- Outing
- Problem 1463 - Come to a spring outing
- hihocoder week 86 Spring Outing【思维】
- WHU1463(Come to a spring outing)背包问题-DP
- Spring Outing 微软2016校园招聘在线笔试第二场
- 工场outing
- 【模拟题,未知算法】hdu hdoj 1761 Spring-outing Decision(2)
- HDU 1707 Spring-outing Decision(输出课程表是否与假期冲突)
- CSU1580: Outing(图论+DP)
- Candy 出去玩 (outing)
- CSU1580: Outing(强连通+拓扑排序+dp)
- Interview chapter1
- Java虚拟机原理图解
- #编译安装了的nginx 添加http_ssl_module模块
- Free browsing:如何阅读源代码/项目:Editor.md
- C++模板运用
- #1154 :Spring Outing
- 块和行 标签
- Eclipse安装Git插件
- 腾讯公司编程风格及个人感悟
- Socket描述符选项[SOL_SOCKET]
- leetcode [Remove Duplicates from Sorted List]
- telnet命令
- final关键字
- (49)Air Band生成自己的级联分类器并进行手势识别(学习版)