[ABC446B] Greedy Draft 题解

AT_abc446_b [ABC446B] Greedy Draft

题目描述

There are $ N $ customers numbered $ 1 $ to $ N $ , and $ M $ canned juices numbered $ 1 $ to $ M $ .

Customer $ i $ ( $ 1 \leq i \leq N $ ) has a wish list of length $ L_i $ . The $ j $ -th item ( $ 1 \leq j \leq L_i $ ) from the top of customer $ i $ 's wish list is canned juice $ X_{i,j} $ . For any customer $ i $ , the numbers $ X_{i, 1}, \dots, X_{i, L_i} $ on customer $ i $ 's wish list are distinct.

Customers $ 1, \dots, N $ , in this order, will now choose their beverages, following the procedure below.

  • If the customer’s wish list contains a canned juice that has not yet been chosen by anyone at that point, they choose the canned juice whose number appears earliest in their wish list. Otherwise, they choose water.

Determine which beverage each customer gets.

输入格式

The input is given from Standard Input in the following format:

$ N $ $ M $ $ L_1 $ $ X_{1,1} $ $ X_{1,2} $ $ \cdots $ $ X_{1,L_1} $ $ L_2 $ $ X_{2,1} $ $ X_{2,2} $ $ \cdots $ $ X_{2,L_2} $ $ \vdots $ $ L_N $ $ X_{N,1} $ $ X_{N,2} $ $ \cdots $ $ X_{N,L_N} $

输出格式

Output $ N $ lines. The $ i $ -th line ( $ 1 \leq i \leq N $ ) should contain the number of the canned juice customer $ i $ gets if they get one, or 0 if customer $ i $ gets water.

输入输出样例 #1

输入 #1

4 5
3
3 1 2
3
3 2 1
2
2 3
4
2 5 3 1

输出 #1

3
2
0
5

输入输出样例 #2

输入 #2

6 5
1
3
2
3 5
5
5 3 1 4 2
5
5 1 3 4 2
5
3 4 1 5 2
5
5 1 3 2 4

输出 #2

3
5
1
4
2
0

说明/提示

Sample Explanation 1

Among the numbers on customer $ 1 $ 's wish list, the canned juices not yet chosen by anyone are $ 3, 1, 2 $ . The one appearing earliest in the list is $ 3 $ , so customer $ 1 $ chooses canned juice $ 3 $ .

Among the numbers on customer $ 2 $ 's wish list, the canned juices not yet chosen by anyone are $ 2, 1 $ . The one appearing earliest in the list is $ 2 $ , so customer $ 2 $ chooses canned juice $ 2 $ .

For the numbers on customer $ 3 $ 's wish list, all corresponding canned juices have already been chosen by someone at that point. Thus, customer $ 3 $ chooses water.

Among the numbers on customer $ 4 $ 's wish list, the canned juices not yet chosen by anyone are $ 5, 1 $ . The one appearing earliest in the list is $ 5 $ , so customer $ 4 $ chooses canned juice $ 5 $ .

Constraints

  • $ 1 \leq N \leq 100 $
  • $ 1 \leq M \leq 100 $
  • $ 1 \leq L_i \leq M $ ( $ 1 \leq i \leq N $ )
  • $ 1 \leq X_{i,j} \leq M $ ( $ 1 \leq i \leq N $ , $ 1 \leq j \leq L_i $ )
  • $ X_{i, 1}, \dots, X_{i, L_i} $ are distinct. ( $ 1 \leq i \leq N $ )
  • All input values are integers.

思路

水题,直接AC。

代码见下

#include<bits/stdc++.h> 
using namespace std;
long long n,m,l[105],x[105][105],ts[105];
bool bo[105];
int main(){
	cin>>n>>m;
	for(int i=1;i<=n;i++){
		cin>>l[i];
		for(int j=1;j<=l[i];j++){
			cin>>x[i][j];
		}
	}
	for(int i=1;i<=n;i++){
		ts[i]=0;
		for(int j=1;j<=l[i];j++){
			if(bo[x[i][j]]==0){
				bo[x[i][j]]=1;
				ts[i]=x[i][j];
				break;
			}
		}
	}
	for(int i=1;i<=n;i++){
		cout<<ts[i]<<endl;
	}
	return 0; 	
}
posted @ 2026-02-22 16:20  bz02_2023f2  阅读(3)  评论(0)    收藏  举报  来源