题意:如果当前人给的数,之前没出现过,并且存在出现过的两数之差和这个数相等,那么这个人就不淘汰,另外三种情况就是淘汰。
题目说了,数是10的5次方,所以直接用数组下标散列,一个hash用来表示这个数出现过没,一个diff用来表示这个差是否出现过。其它按步骤就行了。
#include <bits/stdc++.h>
using namespace std;
const int Maxn = 100005;
int main() {
int x1, x2, n, m, a[15][1005];
scanf("%d %d %d %d", &x1, &x2, &n, &m);
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
scanf("%d", &a[i][j]);
}
}
int hash[Maxn] = {};
int diff[Maxn] = {};
hash[x1] = hash[x2] = diff[abs(x1 - x2)] = 1;
vector<int> v;
v.push_back(x1); v.push_back(x2);
int out[15] = {}, t = 0;
for (int i = 1; i <= m; i++) {
for (int j = 1; j <= n; j++) {
if (!out[j]) {
if (!hash[a[j][i]] && diff[a[j][i]]) {
hash[a[j][i]] = 1;
for (int k = 0; k < v.size(); k++) {
diff[abs(v[k] - a[j][i])] = 1;
}
v.push_back(a[j][i]);
} else {
t++;
out[j] = 1;
printf("Round #%d: %d is out.\n", i, j);
}
}
}
}
if (t == n) printf("No winner.");
else {
printf("Winner(s):");
for (int i = 1; i <= n; i++) {
if (!out[i]) printf(" %d", i);
}
}
return 0;
}