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");
    		}
    	}
    }
}
相关推荐
想不明白的过度思考者2 分钟前
Java从入门到“放弃”(精通)之旅——JavaSE终篇(异常)
java·开发语言
.生产的驴26 分钟前
SpringBoot 封装统一API返回格式对象 标准化开发 请求封装 统一格式处理
java·数据库·spring boot·后端·spring·eclipse·maven
猿周LV34 分钟前
JMeter 安装及使用 [软件测试工具]
java·测试工具·jmeter·单元测试·压力测试
知来者逆36 分钟前
计算机视觉——速度与精度的完美结合的实时目标检测算法RF-DETR详解
图像处理·人工智能·深度学习·算法·目标检测·计算机视觉·rf-detr
晨集36 分钟前
Uni-App 多端电子合同开源项目介绍
java·spring boot·uni-app·电子合同
时间之城38 分钟前
笔记:记一次使用EasyExcel重写convertToExcelData方法无法读取@ExcelDictFormat注解的问题(已解决)
java·spring boot·笔记·spring·excel
阿让啊40 分钟前
C语言中操作字节的某一位
c语言·开发语言·数据结构·单片机·算法
এ᭄画画的北北41 分钟前
力扣-160.相交链表
算法·leetcode·链表
椰羊~王小美1 小时前
LeetCode -- Flora -- edit 2025-04-25
java·开发语言
凯酱1 小时前
MyBatis-Plus分页插件的使用
java·tomcat·mybatis