CF 1438B. Valerii Against Everyone

该博客探讨如何解决一个关于数组的问题,即在给定的数组a中,定义了ai=2bi的关系,判断是否存在两个非重叠区间其元素和相等,Valeri认为这样的区间必须和不同。通过分析元素的独特二进制特性,作者展示了如何判断Valeri的结论是否正确。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

You’re given an array b of length n. Let’s define another array a, also of length n, for which ai=2bi (1≤i≤n).

Valerii says that every two non-intersecting subarrays of a have different sums of elements. You want to determine if he is wrong. More formally, you need to determine if there exist four integers l1,r1,l2,r2 that satisfy the following conditions:

1≤l1≤r1<l2≤r2≤n;
al1+al1+1+…+ar1−1+ar1=al2+al2+1+…+ar2−1+ar2.
If such four integers exist, you will prove Valerii wrong. Do they exist?

An array c is a subarray of an array d if c can be obtained from d by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end.

Input
Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤100). Description of the test cases follows.

The first line of every test case contains a single integer n (2≤n≤1000).

The second line of every test case contains n integers b1,b2,…,bn (0≤bi≤109).

Output
For every test case, if there exist two non-intersecting subarrays in a that have the same sum, output YES on a separate line. Otherwise, output NO on a separate line.

Also, note that each letter can be in any case.

Example
inputCopy
2
6
4 3 0 1 2 0
2
2 5
outputCopy
YES
NO
Note
In the first case, a=[16,8,1,2,4,1]. Choosing l1=1, r1=1, l2=2 and r2=6 works because 16=(8+1+2+4+1).

In the second case, you can verify that there is no way to select to such subarrays.


题意

给你一个长度为 n n n 的数组 b b b,按 a i = 2 b i a_i=2^{b_i} ai=2bi 形式表示成数组 a a a,问能否在数组 a a a 中找到两个区间使得这两个区间和相等。

思路

情况1: a a a 数组中含有相等的数,那么我就可以选择这两个数作为两个区间。

情况2: a a a 数组中的数各不等,由于 a i a_i ai 的二进制表示中只有 b i b_i bi 位是 1 1 1 其他位全是 0 0 0,那么 b i b_i bi 不同就意味着 a i a_i ai 每个数的和都不存在进位,也就不存在区间相等情况。


#include<bits/stdc++.h>
using namespace std;
typedef pair<int, int> PII;
typedef long long ll;

int main()
{
	int t; cin >> t;
	
	while (t--)
	{
		int n; cin >> n;
		unordered_map<int, bool> h;
		bool flag = false;

		for (int i = 0; i < n; i++)
		{
			int x; cin >> x;
			if (h[x]) flag = true;
			else h[x] = true;
		}

		if (flag) puts("YES");
		else puts("NO");
	}


	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值