OR3. Spring Outing
描述
You class are planning for a spring outing. N people are voting for a destination out of K candidate places.输入描述
The first line contains two integers, N and K, the number of people in your class and the number of candidate places.输出描述
Output the selected place. Or "otaku" without quotes if no place is 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; }