首页 > 其他分享 >Gardener and the Array

Gardener and the Array

时间:2023-01-12 10:22:32浏览次数:62  
标签:case elements int subsequences test Array Yes Gardener

题目链接

题目描述:

The gardener Kazimir Kazimirovich has an array of \(n\) integers \(c_1,c_2,…,c_n\).

He wants to check if there are two different subsequences \(a\) and \(b\) of the original array, for which \(f(a)=f(b)\), where \(f(x)\) is the bitwise OR of all of the numbers in the sequence \(x\).

A sequence \(q\) is a subsequence of \(p\) if \(q\) can be obtained from \(p\) by deleting several (possibly none or all) elements.

Two subsequences are considered different if the sets of indexes of their elements in the original sequence are different, that is, the values of the elements are not considered when comparing the subsequences.

输入描述:

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

The first line of each test case contains one integer \(n (1≤n≤10^5)\) — the size of the array \(c\).

The description of the array \(c\) in this problem is given implicitly to speed up input.

The \((i+1)\)-st of the following n lines of the test case begins with an integer \(k_i (1≤k_i≤10^5)\) — the number of set bits in the number \(c_i\). Next follow \(k_i\) distinct integers \(p_{i,1},p_{i,2},…,p_{i,ki} (1≤p_i≤2⋅10^5)\) —the numbers of bits that are set to one in number \(c_i\). In other words, \(c_i=2^{p_{i,1}}+2^{p_{i,2}}+…+2^{p_{i,ki}}\).

It is guaranteed that the total sum of \(k_i\) in all tests does not exceed \(10^5\).

输出描述:

For each set of input, print "Yes" if there exist two different subsequences for which \(f(a)=f(b)\), and "No" otherwise.

You can output the answer in any case (upper or lower). For example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as positive responses.

样例:

input:

5
3
2 1 5
2 2 4
2 2 3
2
2 1 2
1 2
4
3 1 2 4
2 2 4
4 1 2 5 6
2 2 5
5
3 3 1 2
3 2 5 3
5 7 2 3 1 4
5 1 2 6 3 5
3 2 6 3
2
1 1
1 2

output:

No
Yes
Yes
Yes
No

Note:

It can be proven that in the first test case there are no two different subsequences \(a\) and \(b\) for which \(f(a)=f(b)\).

In the second test case, one of the possible answers are following subsequences: the subsequence a formed by the element at position \(1\), and the subsequence \(b\) formed by the elements at positions \(1\) and \(2\).

In the third test case, one of the possible answers are following subsequences: the subsequence \(a\) formed by elements at positions \(1\), \(2\), \(3\) and \(4\), and the subsequence \(b\) formed by elements at positions \(2\), \(3\) and \(4\).

AC代码:

#include <bits/stdc++.h>

using namespace std;

// 贪心的想,a和b或的数相同的越多他们就越接近
// 那么就让a或上所有的数,只要然后让b去掉一个数
// 只要去掉的那个数的每个位都曾经出现过一次,那么就代表b去掉这个数跟之前没区别
// 这样就可以找到f(a) = f(b);
void solve()
{
	int n;
	scanf("%d", &n);

	vector<vector<int>> a(n);
	map<int, int> b;

	for (int i = 0; i < n; i++)
	{
		int k;
		scanf("%d", &k);

		a[i].resize(k);

		for (int j = 0; j < k; j++)
		{
			scanf("%d", &a[i][j]);

			// 这一位的数出现的次数加1
			b[a[i][j]]++;
		}
	}

	for (int i = 0; i < n; i++)
	{
		bool f = 1;
		for (int j = 0; j < a[i].size(); j++)
		{
			// 如果有一位数只出现过一次,就说明去掉这个数不行
			if (b[a[i][j]] == 1)
			{
				f = 0;
				break;
			}
		}

		if (f)
		{
			printf("Yes\n");
			return;
		}
	}

	printf("No\n");
}

int main()
{
	int T;
	scanf("%d", &T);

	while (T--)
		solve();

	return 0;
}

标签:case,elements,int,subsequences,test,Array,Yes,Gardener
From: https://www.cnblogs.com/KSzsh/p/17045669.html

相关文章