Codeforces Round 1004(Div.2) B. Two Large Bags 补题 + 题解 python



B. Two Large Bags


https://codeforces.com/contest/2067/problem/B

题目描述

time limit per test: 1 second
memory limit per test: 256 megabytes

You have two large bags of numbers. Initially, the first bag contains n n n numbers: a 1 , a 2 , … , a n a_1, a_2, \ldots, a_n a1,a2,,an, while the second bag is empty. You are allowed to perform the following operations:

  • Choose any number from the first bag and move it to the second bag.
  • Choose a number from the first bag that is also present in the second bag and increase it by one.

You can perform an unlimited number of operations of both types, in any order. Is it possible to make the contents of the first and second bags identical?

大致题意:

你有两大包数字。最初,第一个袋子包含 n n n 号: a 1 , a 2 , … , a n a_1, a_2, \ldots, a_n a1,a2,,an,而第二个袋子是空的。您可以执行以下操作:

-从第一个袋子中选择任意数字并将其移动到第二个袋子中。

-从第一个袋子中选择一个数字,这个数字也出现在第二个袋子中,然后加1。

您可以以任意顺序执行这两种类型的无限数量的操作。第一个袋子和第二个袋子的内容物是否可以完全相同?


Input

Each test contains multiple test cases. The first line contains the number of test cases t t t ( 1 ≤ t ≤ 1 0 4 1 \le t \le 10^4 1t104). The description of the test cases follows.

The first line of each test case contains an integer n n n ( 2 ≤ n ≤ 1000 2 \le n \le 1000 2n1000) — the length of the array a a a. It is guaranteed that n n n is an even number.

The second line of each test case contains n n n integers a 1 , a 2 , … , a n a_1, a_2, \ldots, a_n a1,a2,,an ( 1 ≤ a i ≤ n 1 \le a_i \le n 1ain).

It is guaranteed that the sum of n 2 n^2 n2 over all test cases does not exceed 1 0 6 10^6 106.

输入

每个测试包含多个测试用例。第一行包含测试用例的数量 t t t ( 1 ≤ t ≤ 1 0 4 1 \le t \le 10^4 1t104 )。下面是测试用例的描述。

每个测试用例的第一行包含一个整数 n n n ( 2 ≤ n ≤ 1000 2 \le n \le 1000 2n1000 )——数组 a a a 的长度。可以保证 n n n 是偶数。

每个测试用例的第二行包含 n n n 个整数 a 1 , a 2 , … , a n a_1, a_2, \ldots, a_n a1,a2,,an ( 1 ≤ a i ≤ n 1 \le a_i \le n 1ain )。

保证所有测试用例的 n 2 n^2 n2 之和不超过 1 0 6 10^6 106


Output

For each test case, print “YES” if it is possible to equalize the contents of the bags. Otherwise, output “NO”.

You can output each letter in any case (for example, “YES”, “Yes”, “yes”, “yEs”, “yEs” will be recognized as a positive answer).

输出

对于每个测试用例,如果可以平衡袋子的内容,则打印“YES”。否则输出“NO”。
您可以在任何情况下输出每个字母(例如,“YES”,“YES”,“YES”,“YES”,“YES”将被识别为肯定答案)。


Example

Input

9
2
1 1
2
2 1
4
1 1 4 4
4
3 4 3 3
4
2 3 4 4
6
3 3 4 5 3 3
6
2 2 2 4 4 4
8
1 1 1 1 1 1 1 4
10
9 9 9 10 10 10 10 10 10 10

Output

Yes
No
Yes
Yes
No
Yes
No
Yes
Yes

Note

Let’s analyze the sixth test case: we will show the sequence of operations that leads to the equality of the bags. Initially, the first bag consists of the numbers ( 3 , 3 , 4 , 5 , 3 , 3 ) (3, 3, 4, 5, 3, 3) (3,3,4,5,3,3), and the second bag is empty.

  1. In the first operation, move the number 3 3 3 from the first bag to the second. State: ( 3 , 4 , 5 , 3 , 3 ) (3, 4, 5, 3, 3) (3,4,5,3,3) and ( 3 ) (3) (3).
  2. In the second operation, increase the number 3 3 3 from the first bag by one. This operation is possible because the second bag contains the number 3 3 3. State: ( 4 , 4 , 5 , 3 , 3 ) (4, 4, 5, 3, 3) (4,4,5,3,3) and ( 3 ) (3) (3).
  3. In the third operation, move the number 4 4 4 from the first bag to the second. State: ( 4 , 5 , 3 , 3 ) (4, 5, 3, 3) (4,5,3,3) and ( 3 , 4 ) (3, 4) (3,4).
  4. In the fourth operation, increase the number 4 4 4 from the first bag by one. State: ( 5 , 5 , 3 , 3 ) (5, 5, 3, 3) (5,5,3,3) and ( 3 , 4 ) (3, 4) (3,4).
  5. In the fifth operation, move the number 5 5 5 from the first bag to the second. State: ( 5 , 3 , 3 ) (5, 3, 3) (5,3,3) and ( 3 , 4 , 5 ) (3, 4, 5) (3,4,5).
  6. In the sixth operation, increase the number 3 3 3 from the first bag by one. State: ( 5 , 3 , 4 ) (5, 3, 4) (5,3,4) and ( 3 , 4 , 5 ) (3, 4, 5) (3,4,5).

As we can see, as a result of these operations, it is possible to make the contents of the bags equal, so the answer exists.

注意

让我们分析第六个测试用例:我们将展示导致袋子相等的操作序列。
最初,第一个袋子由数字(3,3,4,5,3,3)组成,第二个袋子是空的。
1.在第一个操作中,将编号3从第一个袋子移动到第二个袋子。状态:(3,4,5,3,3)和(3)
2.在第二个操作中,将第一个包的编号3增加1。这个操作是可能的,因为第二个包包含数字3。状态:(4,4,5,3,3)和(3)
3.在第三个操作中,将数字4从第一个袋子移动到第二个袋子。状态:(4,5,3,3)和(3,4)。
4.在第四个操作中,将第一个包的编号4增加1。状态:(5,5,3,3)和(3,4)。
5.在第五个操作中,将数字5从第一个包移动到第二个包。状态:(5,3,3)和(3,4,5)。
6.在第六次操作中,将第一个袋子的数字3加1。状态:(5,3,4)和(3,4,5)。

正如我们所看到的,作为这些操作的结果,可以使袋子的内容相等,因此答案是存在的。



思路分析

  1. 初始条件:第一个袋子包含数组a,第二个袋子为空。
  2. 操作
    • 将一个数字从第一个袋子移到第二个袋子。
    • 将第一个袋子中的某个数字加1(前提是该数字已经在第二个袋子中)。

贪心思路:

每次我们发送一个数字到第二个袋子时,如果我们想要平衡,那么在操作结束时,第一个袋子里必须保留一个相等的数字。我们将第一个包中的这个相等的数字称为“blocked”:不应该再对它执行任何操作。


事实证明,在可能的情况下使用第二个操作总是最优的,而不计算“阻塞”的数字。也就是说,将第一个包中所有相等的 a1数字加1。然后继续处理同样的问题。


为什么呢?假设我们在第一个袋子中保留一些相等的 a1 数,而不增加它们。然后,按照同样的逻辑,我们必须将其中一个转移到第二个袋子中,阻塞相同数量的第一个袋子。但是,如果我们将两个数字都增加 1 ,它们仍然相等,并且仍然可以选择将其中一个转移到第二个袋子中,并将相等的一个放在第一个袋子中。此外,等于 a1 的数字已经在第二个包中。
因此,对所有剩余的等于a1 数选择加1是最优的。然后继续处理数组 a3,a4,…,an 的问题,其中所有的数字都是 >a1 ,这个问题已经可以用类似的方法解决了。
声明:思路来自https://codeforces.com/blog/entry/139415?locale=en


code实现

from collections import Countert = int(input())
for _ in range(t):n = int(input())a = list(map(int, input().split()))flag = 1cnt = Counter(a)  # 计数for i in range(max(a) + 1):if cnt[i] > 2:cnt[i + 1] += cnt[i] - 2  cnt[i] = 2# 总是选择操作2,所有等于ai的数加1,即cnt[i]留下两个,其余推入cnt[i+1]for i in range(max(a) + 1):if cnt[i] % 2 == 1:  # 奇数表示未完全配对flag = 0breakif flag == 0:print("No")else:print("Yes")

END
如果有更多问题或需要进一步的帮助,可以在评论区留言讨论哦!
如果喜欢的话,请给博主点个关注 谢谢

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.rhkb.cn/news/17037.html

如若内容造成侵权/违法违规/事实不符,请联系长河编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

sql盲注脚本

在sqli-labs中的第8题无回显可以尝试盲注的手法获取数据 发现页面加载了3秒左右可以进行盲注 布尔盲注数据库名 import requestsdef inject_database(url):datanamefor i in range(1,15):low 32high 128mid (low high) // 2while low < high:path "id1 and asci…

DeepSeekR1 苹果macbook M1本地可视化运行!

过年了&#xff0c;就带了一台 macbook air 8g&#xff0c;DeepSeekR1的消息还是铺天盖地的来&#xff0c;我就想着在这台电脑上也装一个吧。 经过简单的配置&#xff0c;最终也运行起来了&#xff0c;速度还可以。 我这是首款M系列笔记本&#xff0c;也是现在最低配的 M 系列…

centos 10 离线安装dnf 和 设置dnf镜像源

离线安装dnf可用kimi搜索, centos 使用curl 下载dnf 的rpm包 mkdir ~/dnf_packages cd ~/dnf_packages# CentOS 7 示例 curl -O http://springdale.math.ias.edu/data/puias/unsupported/7/x86_64/dnf-0.6.4-2.sdl7.noarch.rpm curl -O http://springdale.math.ias.edu/data/pu…

Vivado生成edif网表及其使用

介绍如何在Vivado中将模块设为顶层&#xff0c;并生成相应的网表文件&#xff08;Verilog文件和edif文件&#xff09;&#xff0c;该过程适用于需要将一个模块作为顶层设计进行综合&#xff0c;并生成用于其他工程中的网表文件的情况。 例如要将fpga_top模块制作成网表给其它工…

【Python网络爬虫】爬取网站图片实战

【Python网络爬虫】爬取网站图片实战 Scrapying Images on Website in Action By Jackson@ML *声明:本文简要介绍如何利用Python爬取网站数据图片,仅供学习交流。如涉及敏感图片或者违禁事项,请注意规避;笔者不承担相关责任。 1. 创建Python项目 1) 获取和安装最新版…

Python从0到100(八十八):LSTM网络详细介绍及实战指南

前言&#xff1a; 零基础学Python&#xff1a;Python从0到100最新最全教程。 想做这件事情很久了&#xff0c;这次我更新了自己所写过的所有博客&#xff0c;汇集成了Python从0到100&#xff0c;共一百节课&#xff0c;帮助大家一个月时间里从零基础到学习Python基础语法、Pyth…

window patch按块分割矩阵

文章目录 1. excel 示意2. pytorch代码3. window mhsa 1. excel 示意 将一个三维矩阵按照window的大小进行拆分成多块2x2窗口矩阵&#xff0c;具体如下图所示 2. pytorch代码 pytorch源码 import torch import torch.nn as nn import torch.nn.functional as Ftorch.set_p…

python013-基于Python的智能停车系统的设计与实现(源码+数据库+论文+部署讲解等)

&#x1f495;&#x1f495;作者&#xff1a; 爱笑学姐 &#x1f495;&#x1f495;个人简介&#xff1a;十年Java&#xff0c;Python美女程序员一枚&#xff0c;精通计算机专业前后端各类框架。 &#x1f495;&#x1f495;各类成品Java毕设 。javaweb&#xff0c;ssm&#xf…

gitlab无法登录问题

在我第一次安装gitlab的时候发现登录页面是 正常的页面应该是 这种情况的主要原因是不是第一次登录&#xff0c;所以我们要找到原先的密码 解决方式&#xff1a; [rootgitlab ~]# vim /etc/gitlab/initial_root_password# WARNING: This value is valid only in the followin…

无线4G多联机分户计费集中控制系统

拓森无线4G多联机集中控制系统应用于宝龙广场多联机计费集中控制节能改造项目&#xff0c;包括多联机集中控制&#xff0c;分户计费&#xff0c;空调监控管理、告警管理、节能管控、统计报表、能效分析、空调远程开关机等功能。项目的成功实施&#xff0c;不仅提升了维护管理效…

oracle多次密码错误登录,用户锁住或失效

多次输入错误账号查询状态&#xff1a; select username,account_status from dba_users; TEST EXPIRED(GRACE) 密码错误延迟登录&#xff0c;延迟登录还能登录 或者 TEST LOCKED(TIMED) 密码错误锁 TEST EXPIRED(GR…

连通两台VMware虚拟机

连通两台VMware虚拟机 Fairing Winds and Following Seas VMware各模式的区别 在尝试连接之前&#xff0c;我们要搞清楚各模式的区别 简单来说就是&#xff0c;只有桥接模式和NAT模式是可以实现虚拟机联通的&#xff0c;而桥接模式和NAT模式分别对应了 V M w a r e VMware VM…

C++ 容器适配器

文章目录 1. 适配器2. stack和queue2.1 deque2.1.1 deque的底层结构2.1.2 deque如何实现头插和随机访问 2.2 用deque实现栈和队列2.3 deque的优缺点 3. priority_queue 1. 适配器 适配器是什么&#xff1f; 适配器是一种设计模式&#xff0c;实质上就是一种复用&#xff0c;即…

DeepSeek R1本地部署解决,DeepSeek服务繁忙

DeepSeek 本地部署是指将DeepSeek模型下载到本地电脑上&#xff0c;利用电脑的显卡进行数据处理和推理&#xff0c;可以减少网络延迟&#xff0c;提高数据处理和响应速度&#xff0c;从而避免将数据传输到云端&#xff0c;增强了数据的主权和控制&#xff0c;减少了因网络连接可…

GPT和BERT

笔记来源&#xff1a; Transformer、GPT、BERT&#xff0c;预训练语言模型的前世今生&#xff08;目录&#xff09; - B站-水论文的程序猿 - 博客园 ShusenWang的个人空间-ShusenWang个人主页-哔哩哔哩视频&#xff08;RNN模型与NLP应用&#xff09; 一、GPT 1.1 GPT 模型的…

深入浅出Java反射:掌握动态编程的艺术

小程一言反射何为反射反射核心类反射的基本使用获取Class对象创建对象调用方法访问字段 示例程序应用场景优缺点分析优点缺点 注意 再深入一些反射与泛型反射与注解反射与动态代理反射与类加载器 结语 小程一言 本专栏是对Java知识点的总结。在学习Java的过程中&#xff0c;学习…

JDK 14,15,17的一些新特性(部分常用)

1&#xff1a;instanceof&#xff08;后&#xff0c;使用不再需要墙转&#xff09; 2&#xff1a;switch语句增强 1&#xff1a;支持lmbda&#xff0c;自动防击穿&#xff0c;有返回值 2&#xff1a;支持case多个值&#xff0c;复杂逻辑结果支持yield返回 3&#xff1a;字符串…

活字格使用说明书

字格设计使用说明书 目录 1. 数据 2. 页面 3. 组件 4. 命令 一、数据 1.表数据创建(鼠标移动到表右击点击创建表) ‘ 图表 1 鼠标移至表1右击可重命名,添加字段输入所需字段名(一般数据类型的要注意:日期格式字段---日期、ID或者字典字段---整数、金…

springboot021校园周边美食探索及分享平台

版权声明 所有作品均为本人原创&#xff0c;提供参考学习使用&#xff0c;如需要源码数据库配套文档请移步 www.taobysj.com 搜索获取 技术实现 开发语言&#xff1a;Javavue。 框架&#xff1a;后端spingboot前端vue。 模式&#xff1a;B/S。 数据库&#xff1a;mysql。 开…

Kubernetes部署KeyDB服务

Kubernetes YAML 配置文件&#xff0c;部署一个 KeyDB 容器 vi keydb-deployment.yaml内容如下 apiVersion: apps/v1 kind: Deployment metadata:name: keydb-deployment spec:replicas: 1selector:matchLabels:app: keydbtemplate:metadata:labels:app: keydbspec:container…