hihocoder week 86 Spring Outing【思维】

来源:互联网 发布:搜狗手机输入法mac 编辑:程序博客网 时间:2024/06/05 17:14

P1 : Spring Outing

Time Limit:20000ms
Case 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 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.

Sample Input
2 21 0 22 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;}


0 0
原创粉丝点击