列表

详情


OR3. 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?

输入描述

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

示例1

输入:

2 2
1 0 2
2 1 0

输出:

1

原站题解

C++ 解法, 执行用时: 9ms, 内存消耗: 760KB, 提交时间: 2021-01-21

#include <cstdio>

using namespace std;

inline int read() {
	int x = 0;
	char c = getchar();
	while (c > '9' || c < '0') {
		if (c == EOF)
			return -1;;
		c = getchar();
	}
	while (c <= '9' && c >= '0')
		x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
	return x;
}

int main() {
	int n;
	while (~(n = read())) {
		int k = read(),a[n][k + 1];
		for (int i = 0; i < n; ++i)
			for (int j = 0; j <= k; ++j)
				a[i][read()] = j;
		int r = (n & 1) ? n / 2 : n / 2 - 1, ans = 0;
		for (int i = k; i > 0; --i) {
			int cnt = 0;
			for (int j = 0; j < n; ++j)
				if (a[j][i] > a[j][ans] && ++cnt > r)
					break;
			if (cnt <= r)
				ans = i;
		}
		ans ? printf("%d\n", ans) : puts("otaku");
	}
}

C++ 解法, 执行用时: 10ms, 内存消耗: 8568KB, 提交时间: 2017-06-09

#include <cstdio>
    
int main() {
    int n, k;
    while (scanf(" %d %d", &n, &k) != EOF) {
        int prefer[n][k+1];
        int val;
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < k+1; ++j) {
                scanf(" %d", &val);
                prefer[i][val] = j; //i同学把第val个地方排在第j名
            }
        }
        int place = 0;
        //根据第j+1轮的选择倒推第j轮的选择
        for (int j = k; j >= 1; --j) {
            int n_choose = 0;//选择第j个地方的人数
            for (int i = 0; i < n; ++i) {
                if (prefer[i][j] < prefer[i][place]) {
                    n_choose++;
                }
            }
            if (n_choose > n / 2) {
                place = j;
            }
        }
        if (place == 0)
            printf("otaku\n");
        else
            printf("%d\n", place);
    }
    return 0;
}

C++ 解法, 执行用时: 10ms, 内存消耗: 8568KB, 提交时间: 2017-03-31

#include <cstdio>
   
int main() {
    int n, k;
    while (scanf(" %d %d", &n, &k) != EOF) {
        int prefer[n][k+1];
        int val;
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < k+1; ++j) {
                scanf(" %d", &val);
                prefer[i][val] = j; //i同学把第val个地方排在第j名
            }
        }
        int place = 0;
        //根据第j+1轮的选择倒推第j轮的选择
        for (int j = k; j >= 1; --j) {
            int n_choose = 0;//选择第j个地方的人数
            for (int i = 0; i < n; ++i) {
                if (prefer[i][j] < prefer[i][place]) {
                    n_choose++;
                }
            }
            if (n_choose > n / 2) {
                place = j;
            }
        }
        if (place == 0)
            printf("otaku\n");
        else
            printf("%d\n", place);
    }
    return 0;
}

C++ 解法, 执行用时: 10ms, 内存消耗: 8568KB, 提交时间: 2016-08-13

#include <cstdio>
  
int main() {
    int n, k;
    while (scanf(" %d %d", &n, &k) != EOF) {
        int prefer[n][k+1];
        int val;
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < k+1; ++j) {
                scanf(" %d", &val);
                prefer[i][val] = j; //i同学把第val个地方排在第j名
            }
        }
        int place = 0;
        //根据第j+1轮的选择倒推第j轮的选择
        for (int j = k; j >= 1; --j) {
            int n_choose = 0;//选择第j个地方的人数
            for (int i = 0; i < n; ++i) {
                if (prefer[i][j] < prefer[i][place]) {
                    n_choose++;
                }
            }
            if (n_choose > n / 2) {
                place = j;
            }
        }
        if (place == 0)
            printf("otaku\n");
        else
            printf("%d\n", place);
    }
    return 0;
}

C++ 解法, 执行用时: 10ms, 内存消耗: 8568KB, 提交时间: 2016-03-31

#include <cstdio>
 
int main() {
    int n, k;
    while (scanf(" %d %d", &n, &k) != EOF) {
        int prefer[n][k+1];
        int val;
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < k+1; ++j) {
                scanf(" %d", &val);
                prefer[i][val] = j; //i同学把第val个地方排在第j名
            }
        }
        int place = 0;
        //根据第j+1轮的选择倒推第j轮的选择
        for (int j = k; j >= 1; --j) {
            int n_choose = 0;//选择第j个地方的人数
            for (int i = 0; i < n; ++i) {
                if (prefer[i][j] < prefer[i][place]) {
                    n_choose++;
                }
            }
            if (n_choose > n / 2) {
                place = j;
            }
        }
        if (place == 0)
            printf("otaku\n");
        else
            printf("%d\n", place);
    }
    return 0;
}

上一题