2020年7月第2題 PAT甲級真題 The Judger (25分)

baixiaofei567發表於2020-11-30

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.

第二次作者道題了,心情炸裂,四個月和沒有進步一樣,真的很難受
每一輪判斷這個人說的數字是否是前面記錄的任意兩個數字的差,且沒有出現過,那就算過關。不然就淘汰,且後面也不用再參加了。
說真的,沒有set和find函式,我用vector一個個遍歷估計和個傻逼一樣。
這裡用的是unordered_set,可以減小查詢時間,防止超時,和find()函式絕配。將前兩個數字先加入s中,然後每一輪判斷每個人說的數字,如果這個人說的數字在s中可以被find到或者不是任意兩個數的差。任意兩個數的差用unordered_set的迭代器來,遍歷容器內的每一個數字,如果找得到x+當前迭代器的值,證明x就是這兩個數的差了,就return false。每次it++,都要找一次。如果這個人不合格,就將它insert進bad中,然後輸出它被淘汰的資訊。反之,將這個數加入s中
最後for迴圈遍歷每個人,看看能不能在bad中找到它,如果不能,證明這個人是winner
其實set就是用來find的,比vector好用多了!!!set是有序的,unordered_set是無序的,find的時間複雜度是O(logN),比vector的for迴圈查詢好多了

//記錄每個人每輪的發言,如果這個人out了,它的發言不會被記錄,且接下來到它的回合直接continue
//判斷這個人的數字是否出現過且判斷這個數是否是前面任意數的差值
//任意數的差值,是否找得到當前判斷的這個數+當前的迭代器的數 
#include<iostream>
#include<unordered_set>
#include<cstdio>
using namespace std;
unordered_set<int> s;
unordered_set<int> bad;
int a[15][1005];

bool check(int x){
	for(unordered_set<int>::iterator it = s.begin(); it!=s.end();it++){
		if(s.find(x + *it) != s.end()) return true;
	}
	return false;
}

int main(){
	int s1,s2,n,m;
	cin>>s1>>s2>>n>>m;
	for(int i = 0; i < n; i++){
		for(int j = 0; j < m; j++){
			cin>>a[i][j];
		}
	}
	s.insert(s1);s.insert(s2);
	for(int i = 0; i <m; i++){//i是第幾輪 
		for(int j = 0; j < n; j++){
			if(bad.find(j) != bad.end()) continue;
			if(s.find(a[j][i])!= s.end()||!check(a[j][i])){
				bad.insert(j);
				printf("Round #%d: %d is out.\n",i+1,j+1);
			}
			else{
				s.insert(a[j][i]);
			}
		}
	}
	bool flag = true;
	for(int i = 0; i < n; i++){
		if(bad.find(i) == s.end()){
			if(flag == true){
				printf("Winner(s): %d",i+1);
				flag = false;
			}
			else{
				printf(" %d",i+1);
			}
		}
	}
	if(flag == true) cout<<"No Winner";
	return 0;
}

相關文章