Spring Outing

題目描述
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?


IDEA

i表示第i個人,從0->n-1;

p表示第p個地方,從1->k,0表示那都不選

prefer[i][p]表示表示在i心中選擇p的排位爲j,j從0->k

從j=k開始到1,若果出現選擇人數大於總人數一般的,則標記place爲該地方


CODE

#include<iostream>
#include<fstream>
#include<vector>
#include<algorithm>
using namespace std;
int main(){
	#ifndef ONLINE_JUDGE
	freopen("input.txt","r",stdin);
	#endif 
	int n,k;
	while(cin>>n>>k){	
		vector<vector<int> > prefer;
		prefer.resize(n,vector<int> (k+1));
		for(int i=0;i<n;i++){
			for(int j=0;j<k+1;j++){
				int p; cin>>p;
				prefer[i][p]=j;
			}
		}
		int place=0;
		for(int j=k;j>=1;j--){
			int cnt=0;
			for(int i=0;i<n;i++){
				if(prefer[i][j]<prefer[i][place]){
					cnt++;
				}
			}
			if(cnt>n/2){
				place=j;
			}
		}
		if(place==0){
			cout<<"otaku"<<endl;
		}else{
			cout<<place<<endl;
		}		
	}
	return 0;
} 


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章