A game of numbers has the following rules: at the beginning, two distinct positive integers are given by the judge. Then each player in turn must give a number to the judge. The number must be the difference of two numbers that are previously given, and must not be duplicated to any of the existed numbers. The game will run for several rounds. The one who gives a duplicate number or even a wrong number will be kicked out.
Your job is to write a judger program to judge the players' numbers and to determine the final winners.
Input Specification:
Each input file contains one test case. For each case, the first line gives two distinct positive integers to begin with. Both numbers are in [1,10^5].
In the second line, two numbers are given: N (2≤N≤10), the number of players, and M (2≤M≤10^3), the number of rounds.
Then N lines follow, each contains M positive integers. The i-th line corresponds to the i-th player (i=1,⋯,N). The game is to start from the 1st player giving his/her 1st number, followed by everybody else giving their 1st numbers in the 1st round; then everyone give their 2nd numbers in the 2nd round, and so on so forth.
Output Specification:
If the i
-th player is kicked out in the k
-th round, print in a line Round #k: i is out.
. The rest of the numbers given by the one who is out of the game will be ignored. If more than one player is out in the same round, print them in increasing order of their indices. When the game is over, print in the last line Winner(s): W1 W2 ... Wn
, where W1 ... Wn
are the indices of the winners in increasing order. All the numbers in a line must be separated by exactly one space, and there must be no extra space at the beginning or the end of the line. If there is no winner, print No winner.
instead.
Sample Input 1:
101 42
4 5
59 34 67 9 7
17 9 8 50 7
25 92 43 26 37
76 51 1 41 40
Sample Output 1:
Round #4: 1 is out.
Round #5: 3 is out.
Winner(s): 2 4
Sample Input 2:
42 101
4 5
59 34 67 9 7
17 9 18 50 49
25 92 58 1 39
102 32 2 6 41
Sample Output 2:
Round #1: 4 is out.
Round #3: 2 is out.
Round #4: 1 is out.
Round #5: 3 is out.
No winner.
题目大意:首先给定两个不同的正整数,然后参加游戏的几个人轮流给出正整数。每个人给出的数字必须是前面已经出现的某两个正整数之差,且不能等于之前的任何一个数。游戏一直持续若干轮,中间有写重复或写错的人就出局。判断每位游戏者给出的数字是否合法,以及最后的赢家。
分析:要记录的值有几个。1、已经出现过的正整数值,用于计算之后哪些正整数是合法的;2、哪些玩家还没有出局;3、当前有哪些数字是合法的,可以出现。
按轮次对比每个没有出局的玩家给出的数字是否合法即可。
#include<algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <string>
#include <vector>
#include <cstdio>
#include <queue>
#include <stack>
#include <ctime>
#include <cmath>
#include <map>
#include <set>
#define INF 0xffffffff
#define db1(x) cout<<#x<<"="<<(x)<<endl
#define db2(x,y) cout<<#x<<"="<<(x)<<", "<<#y<<"="<<(y)<<endl
#define db3(x,y,z) cout<<#x<<"="<<(x)<<", "<<#y<<"="<<(y)<<", "<<#z<<"="<<(z)<<endl
#define db4(x,y,z,r) cout<<#x<<"="<<(x)<<", "<<#y<<"="<<(y)<<", "<<#z<<"="<<(z)<<", "<<#r<<"="<<(r)<<endl
#define db5(x,y,z,r,w) cout<<#x<<"="<<(x)<<", "<<#y<<"="<<(y)<<", "<<#z<<"="<<(z)<<", "<<#r<<"="<<(r)<<", "<<#w<<"="<<(w)<<endl
using namespace std;
int main(void)
{
#ifdef test
freopen("in.txt","r",stdin);
//freopen("in.txt","w",stdout);
clock_t start=clock();
#endif //test
int left,right,n,m;scanf("%d%d%d%d",&left,&right,&n,&m);
int num[n+5][m+5];
for(int i=1;i<=n;++i)
for(int j=1;j<=m;++j)
scanf("%d",&num[i][j]);
int t=2;
int temp[n*m+5]={left,right},f[n+5]={0},cnt[100010]={0};//cnt标记可以出现的值,f表示当前玩家是否出具,temp存储已经出现过的值
cnt[(int)fabs(right-left)]=1,cnt[left]=cnt[right]=-1;//注意初始的两个值也不能再出现了
for(int j=1;j<=m;++j)
{
for(int i=1;i<=n;++i)
{
if(f[i]==0)
{
int val=num[i][j];
if(cnt[val]==1)
{
temp[t]=val,cnt[val]=-1;
for(int k=0;k<t;++k)
{
int x=fabs(temp[k]-val);
if(cnt[x]!=-1)cnt[x]=1;
}
t++;
}
else
{
f[i]=-1;
printf("Round #%d: %d is out.\n",j,i);
}
}
}
}
int flag=0;
for(int i=1;i<=n;++i)
{
if(f[i]==0)
{
if(!flag)printf("Winner(s):"),flag=1;
printf(" %d",i);
}
}
if(!flag)printf("No winner.");
printf("\n");
#ifdef test
clockid_t end=clock();
double endtime=(double)(end-start)/CLOCKS_PER_SEC;
printf("\n\n\n\n\n");
cout<<"Total time:"<<endtime<<"s"<<endl; //s为单位
cout<<"Total time:"<<endtime*1000<<"ms"<<endl; //ms为单位
#endif //test
return 0;
}