题目3 : Spring Outing 微软2016校园招聘在线笔试第二场

题目3 : Spring Outing

时间限制:20000ms
单点时限:1000ms
内存限制:256MB

描述

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.

样例输入
2 2
1 0 2
2 1 0
样例输出
1
分析:
从后往前推。首先,应该想到的是,至少有一个选择是留在家里的,当前面的所有都不通过,留在家里是必需的选择。于是,初始ans=0;
那么,从后往前推,因为投票是从第一个开始的。一个人会投前面的票的情况是,当它的一个较差的选择都能满足时才会投当前的票,否则,如果他知道他一个较好的较后的选择能满足时,他不会投当前的票。扫描每个人的喜好,比较当前的满足的的一定被选的地方与当前投票的地方相对于这个人的喜好程度,更喜好则投票,否则不投。为什么是要只与一个后面最近的满足的地方比较呢?因为最近的满足了,后面的就不会再进行投票了,就不会再考虑。
代码:
#include <iostream>
#include <cstring>
#include <cstdio>
#define lowbit(x) (x&(-x))
using namespace std;

int map[1005][1005];

int n,k,t;

int main(){

	while(scanf("%d%d",&n,&k)!=EOF){
		for(int i=1;i<=n;i++){
			for(int j=1;j<=k+1;j++){
				scanf("%d",&t);
				map[i][t]=j;
			}
		}
		int ans=0;
		for(int j=k;j>=1;j--){
			int cnt=0;
			for(int i=1;i<=n;i++){
				if(map[i][j]<map[i][ans])
				cnt++;
			}
			if(cnt>n/2)
			ans=j;
		}
		if(ans>0)
		printf("%d
",ans);
		else printf("otaku
");
		
	}
	return 0;
}

  

原文地址:https://www.cnblogs.com/jie-dcai/p/4504872.html