2020年7月第2題 PAT甲級真題 The Judger (25分)
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,105].
In the second line, two numbers are given: N (2≤N≤10), the number of players, and M (2≤M≤103), 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;
}
相關文章
- PAT甲級真題1069 數字黑洞(巧妙解法)
- PAT甲級考試題庫題目分類
- 2024 秋季PAT認證甲級(題解A1-A4)
- PAT甲級1032 Sharing
- 19年春季第二題 PAT甲級 1157 Anniversary(25 分)
- PAT甲級1030 Travel Plan
- 浙大PAT甲級考試
- 2019年9月8日秋季PAT甲級題解-2-1161-Merging Linked Lists (25 分)
- PAT甲級1023 Have Fun with Number
- PAT 甲級 1152 Google Recruitment (20分)GoUI
- 20年春季甲級pat考試
- PAT甲級-1015. Reversible Primes (20)
- PAT甲級1126~1130|C++實現C++
- PAT甲級-1014. Waiting in Line (30)(模擬)AI
- 【PAT甲級A1084】Broken Keyboard (20分)(c++)C++
- 【PAT甲級A1038】Recover the Smallest Number (30分)(c++)C++
- PAT甲級1122 Hamiltonian Cycle (25分)|C++實現C++
- PAT甲級1154 Vertex Coloring (25分)|C++實現C++
- PAT甲級-1140. Look-and-say Sequence (20)(模擬)
- 2021.9.12週六PAT甲級考試覆盤與總結
- PAT甲級1110 Complete Binary Tree (25分)|C++實現C++
- 四劍客面試真題-2面試
- PAT-A1119 題解
- 菜鳥記錄:c語言實現PAT甲級1010--RadixC語言
- 【PAT甲級A1065】A+B and C (64bit) (20分)(c++)C++
- (非原創)PAT甲級1123 Is It a Complete AVL Tree (30分)|C++實現C++
- PAT(甲級)2020年秋季考試 7-1 Panda and PP Milk (20分)
- Java計算機二級(上機真題)Java計算機
- Java常見面試真題之中級進階Java面試
- 微課|玩轉Python輕鬆過二級:第2章課後習題解答(3課,79題)Python
- csp-s真題題解
- 2019年6月四六級備考資料/四六級真題分享
- PAT 乙級
- 問題 D: NOIP2004普及組第2題 花生採摘
- PTA甲級——Be Unique
- LeetCode題解第122題LeetCode
- Java常見面試真題之中級進階(HashMap篇)Java面試HashMap
- Java常見面試真題之中級進階(List篇)Java面試