题目描述
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.
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;
}