1169. The Judger (25)-PAT甲级真题 (哈希表)

题意

首先给出两个数字a, b;然后n个人分别给出自己1-m轮猜的数字。每轮顺序检测参与和提供的数字是否与此前重复,同时是否是前面各处的两个数之差。如果不满足上述条件,则该同学出局,该同学此后(包含此次)猜的每一个数字都忽略。按照格式输出每轮淘汰的人员,最后输出获胜者。
注意:The number must be the difference of two numbers that are previously given:这个数必须是前面给出的两个数之差。

思路

  • 建立两个哈希表exi、exi2
  • 矩阵rec存图,转置后遍历
  • 首先判断i是否已被淘汰if(st[i]) continue;然后检查rec[i][j]的合法性,非法则淘汰ist[i] = true;,合法则更新exi和exi2
  • 每轮结束后按行输出淘汰人员(若有)。
  • 最后根据st数组记录胜利人员名单并输出。

总结

该题对于此前出现数字的记录数组pro,用vector要快于unordered_set,为什么?

题解

//13:52 vector < unordered< set
#include<bits/stdc++.h>
using namespace std;
int a, b, n, m, rec[11][1001];
bool exi[100001], exi2[100001];
vector<int> pro;
bool st[11];
int main(){
	cin>>a>>b;
	exi[a] = exi[b] = true;
	exi2[abs(a - b)] = true;
	pro.push_back(a), pro.push_back(b);
	cin>>n>>m;
	for(int i = 1; i <= n; i ++)
		for(int j = 1; j <= m; j ++)
			scanf("%d", &rec[i][j]);
	for(int j = 1; j <= m; j ++){
		set<int>ans;
		for(int i = 1; i <= n; i ++){
			if(st[i]) continue;
			if(exi[rec[i][j]] || !exi2[rec[i][j]]){
				st[i] = true;
				ans.insert(i);
				continue;
			}
			exi[rec[i][j]] = true;
			pro.push_back(rec[i][j]); 
			for(auto tar : pro) exi2[abs(rec[i][j] - tar)] = true;
		}
		if(ans.size())
			for(auto k : ans) printf("Round #%d: %d is out.\n", j, k);
	}
	vector<int>ans;
	for(int i = 1; i <= n; i ++)
		if(!st[i]) ans.push_back(i);
	if(!ans.size()) printf("No winner.");
	else{
		printf("Winner(s):");
		for(auto i : ans) printf(" %d", i);
	}
	return 0;
}

题目

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.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值