Codeforces Round 923 (Div. 3) C. Choose the Different Ones(Java)

比赛链接:Round 923 (Div. 3)

C题传送门:C. Choose the Different Ones!

题目:

** Example**

** input**

复制代码
6
6 5 6
2 3 8 5 6 5
1 3 4 10 5
6 5 6
2 3 4 5 6 5
1 3 8 10 3
3 3 4
1 3 5
2 4 6
2 5 4
1 4
7 3 4 4 2
1 4 2
2
6 4 4 2
1 5 2
3
2 2 1 4 3

output

复制代码
YES 
NO
YES
YES
NO
NO

分析:

题目要我们判断从a[i]和b[i]中分别选k/2个元素,以便所选元素包含从 1 到 k 的每个整数。

我们可以定义3个变量cnt0,cnt1,com,每个相同数字只计算一次,cnt0是只存在 a[i]中1-k整数的个数,cnt1是只存在 b[i]中1-k整数的个数,com是共同存在a[i]和b[i]中1-k整数的个数。

定义二维数组b[k+10][2],b[i][0]记录a[i]中1-k整数是否存在,b[i][1]记录b[i]中1-k整数是否存在,当b[i][0]==b[i][1]=1时,说明有共同的数。

最后我们通过cnt0,cnt1,com,k的数量关系可以判断结果。

代码:

java 复制代码
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {   
    	Scanner sc = new Scanner(System.in);
    	int tt = sc.nextInt();
    	while(tt-->0) {
    		int n = sc.nextInt();
    		int m = sc.nextInt();
    		int k = sc.nextInt();
    		int [][] b = new int [k+10][2];
    		int cnt0 = 0;int cnt1 = 0;int com = 0;
    		for(int i = 0;i < n;i++) {
    			int t = sc.nextInt();
    			if(t<=k&&b[t][0]==0) {
    				b[t][0]=1;cnt0++;
    			}
    		}
    		//System.out.println(cnt0);
    		for(int i = 0;i < m;i++) {
    			int t = sc.nextInt();
    			if(t<=k&&b[t][1]==0) {
    				b[t][1]=1;cnt1++;
    				if(b[t][0]==1) {
    					cnt0--;cnt1--;com++;
    				}
    			}
    		}
    		//System.out.println(cnt0+" "+cnt1+" "+com);
    		if(cnt0+com<k/2||cnt1+com<k/2||cnt1+cnt0+com<k) {
    			System.out.println("NO");
    		}else if(com>0&&cnt0+com==k/2&&cnt1+com==k/2) {
    			System.out.println("NO");
    		}else {
    			System.out.println("YES");
    		}
    	}
    }
}
相关推荐
极客先躯16 分钟前
高级java每日一道面试题-2025年11月24日-容器与虚拟化题[Dockerj]-runc 的作用是什么?
java·oci 的命令行工具·最小可用·无守护进程·完全标准·创建容器的核心流程·runc 核心职责思维导图
用户606487671889621 分钟前
AI 抢不走的技能:用 Claude API 构建自动化工作流实战
java
我命由我1234533 分钟前
Kotlin 开发 - lateinit 关键字
android·java·开发语言·kotlin·android studio·android-studio·android runtime
aXin_ya34 分钟前
微服务第八天 Sentinel 四种分布式事务模式
java·数据库·微服务
智者知已应修善业35 分钟前
【51单片机2个按键控制流水灯运行与暂停】2023-9-6
c++·经验分享·笔记·算法·51单片机
Halo_tjn37 分钟前
Java Set集合相关知识点
java·开发语言·算法
Linsk43 分钟前
Java和JavaScript的关系真是雷峰和雷峰塔的关系吗?
java·javascript·oracle
许彰午1 小时前
我手写了一个 Java 内存数据库(二):B+ 树的插入与分裂
java·开发语言·面试
zhouwy1131 小时前
Java 快速入门笔记:从基础语法到 Spring Boot 实战
java
极创信息1 小时前
信创产品认证怎么做?信创产品测试认证的主要流程
java·大数据·数据库·金融·软件工程