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

分析:

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

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

定义二维数组bk+102,bi0记录ai中1-k整数是否存在,bi1记录bi中1-k整数是否存在,当bi0==bi1=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");
    		}
    	}
    }
}
相关推荐
尚早立志4 分钟前
Spring Boot 源码研读之ConfigurableEnvironment 环境准备
java·spring boot·后端
YuK.W4 分钟前
Leetcode100: 94.二叉树中序遍历、104.二叉树最大深度、226.翻转二叉树
java·算法·leetcode·二叉树
乂爻yiyao11 分钟前
0. openems 部署与体验
java·openems
TanYYF18 分钟前
spring ai入门教程一
java·人工智能·spring
掉鱼的猫24 分钟前
用 ChatModel 构建 LLM 驱动的 Java 应用
java·llm
41541126 分钟前
JTS 空间运算实战:线 × 线、线 × 面、面 × 面叠加分析
java·jts·叠加分析
.Hypocritical.43 分钟前
数据结构笔记——链表成环/反转 + 有序二叉树(BST)构建、遍历、删除
java·数据结构
只会写代码1 小时前
一套开箱即用实体反射Lambda链式工具,彻底告别原生反射样板代码
java·程序员·源码
AI人工智能+电脑小能手1 小时前
【大白话说Java面试题 第151题】【06_Spring篇】第11题:说一下 Spring Bean 的生命周期?
java·开发语言·后端·spring·面试
骑士雄师1 小时前
java面试题:jvm ,mybatis
java·jvm·mybatis