Quantcast
Channel: xdlove
Viewing all articles
Browse latest Browse all 4

微软笔试题《Spring Outing》

$
0
0

题目1 : 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
题意:题目的意思是有n个班级打算出去春游,他们有k个旅游地点选,也可以选择不去。现在领导要统计一下每个班级的意愿,并且公布出来。即每个班级可以看到其它班级的意愿。每个班级的人都是绝顶聪明的,他们会尽可能的让自己想去的地方被选上。领导会从第一个地点开始,n个班级投票,如果超过一半的班级投赞同票,该地点就会成为春游的地点,投票结束,否则继续投,如果最终没有一个地点被选上,则全部呆在家里。
题解:题目是从第一个地点开始投票,如果我们从第一个地点开始分析,将不能够明确每个班级的意愿,假如我们在最后一个地点开始分析起,即地点k。此时知道,对于每一个班级来说,如果它们想去k地点的意愿大于呆在家里的意愿,则肯定会投赞同票,相反,则投反对票,于是乎,我们可以确定地点k的最终结果,不是呆在家,就是去地点k。然后我们类比k-1个地点,因为地点k已经确定了结果,设为Result[k],那么第k-1地点的结果自然也就可以分析出来,最后的答案就是Result[1];
#include <iostream>
#include <vector>
#include <string.h>
#include <string>
#include <stack>
#include <algorithm>

using namespace std;

int Rank[1005][1005];
int Result[1005];

void outputSelectPlaceOrOtaku() {
    int n, k;
    cin >> n >> k;
    for (int i = 1; i <= n; ++i) {
        for (int j = 1; j <= k + 1; ++j) {
            int x;
            cin >> x;
            Rank[i][x] = j;
        }
    }
    Result[k + 1] = 0;
    for (int i = k; i >= 1; --i) {
        int countVoteAgainst = 0;
        for (int j = 1; j <= n; ++j) {  
            if (Rank[j][Result[i + 1]] < Rank[j][i]) {
                countVoteAgainst++;
            }
        }
        Result[i] = Result[i + 1];
        if (n - countVoteAgainst > n / 2) {
            Result[i] = i;
        }
    }
    if(Result[1] != 0) cout << Result[1] << endl;
    else cout << "otaku" << endl;
}

int main() {
    outputSelectPlaceOrOtaku();
    return 0;
}

 

Viewing all articles
Browse latest Browse all 4