CF 1890A Doremy‘s Paint 3 学习笔记 map的使用

原题

A. Doremy's Paint 3

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

An array 𝑏1,𝑏2,...,𝑏𝑛�1,�2,...,�� of positive integers is good if all the sums of two adjacent elements are equal to the same value. More formally, the array is good if there exists a 𝑘� such that 𝑏1+𝑏2=𝑏2+𝑏3=...=𝑏𝑛−1+𝑏𝑛=𝑘�1+�2=�2+�3=...=��−1+��=�.

Doremy has an array 𝑎� of length 𝑛�. Now Doremy can permute its elements (change their order) however she wants. Determine if she can make the array good.

Input

The input consists of multiple test cases. The first line contains a single integer 𝑡� (1≤𝑡≤1001≤�≤100) --- the number of test cases. The description of the test cases follows.

The first line of each test case contains a single integer 𝑛� (2≤𝑛≤1002≤�≤100) --- the length of the array 𝑎�.

The second line of each test case contains 𝑛� integers 𝑎1,𝑎2,...,𝑎𝑛�1,�2,...,�� (1≤𝑎𝑖≤1051≤��≤105).

There are no constraints on the sum of 𝑛� over all test cases.

Output

For each test case, print "Yes" (without quotes), if it is possible to make the array good, and "No" (without quotes) 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.

Example

input

Copy

复制代码

5

2

8 9

3

1 1 2

4

1 1 4 5

5

2 3 3 3 3

4

100000 100000 100000 100000

output

Copy

复制代码
Yes
Yes
No
No
Yes

Note

In the first test case, [8,9][8,9] and [9,8][9,8] are good.

In the second test case, [1,2,1][1,2,1] is good because 𝑎1+𝑎2=𝑎2+𝑎3=3�1+�2=�2+�3=3.

In the third test case, it can be shown that no permutation is good.

链接

传送门

代码

cpp 复制代码
#include<bits/stdc++.h>
using namespace std;

int main()
{
	int t;
	scanf("%d",&t);
	
	while(t--)
	{
		int n;
		scanf("%d",&n);
		
		map<int,int> occ;
		for(int i=0;i<n;i++)
		{
			int x;
			scanf("%d",&x);
			occ[x]++;
		}
		
		if(occ.size()>=3)	puts("No");
		else
		{
			if(abs(occ.begin()->second-occ.rbegin()->second)<=1)
				puts("Yes");
			else
				puts("No");
		}
	}
	
	return 0;
}

总结

1.题目的意思是,输入一个数列,询问,能否经过若干次交换顺序,使得,该数列变成这样的数列:任意两个相邻的元素之和相等

2.如果要满足任意两个相邻元素之和相等,可以这样来考虑,

a1+a2=a2+a3

a2+a3=a3+a4

a3+a4=a4+a5

a4+a5=a5+a6

观察上面的式子,可以发现,a1=a3=a5

a2=a4=a6

也就是说经过改变顺序之后,数列元素间隔一项,两个元素相等,假设有偶数个元素,就意味着下标是奇数的元素都相等,下标是偶数的元素都相等

如果有奇数个元素,12121,比如说有5个元素,如果仍然满足间隔一项相等,还是可以满足要求

偶数:n/2个奇数下标,n/2个偶数下标

奇数:n/2个奇数下标,n-n/2个偶数下标(注意这里的除法是向下取整的除法)

或者 n/2个偶数下标,n-n/2个奇数下标(除法是向下取整的)

3.如果有3个不同的元素,无论如何不可以满足要求,比如说1,2,3,任意两个相邻元素之和都不会相等,无法满足要求

4.可以记录某一个数字出现的次数,比较这两个数字出现的次数,从而判断能否经过修改,使得满足题目要求。如果两个数字出现次数相同,表示是1212这种情况,如果两个数字出现次数之差等于1,表示12121这种情况

5.另外的情况不满足题目要求

6.map可以存两个数据,第一个数据也就是所谓的key,存的是数值,第二个数据存的是该数值出现的次数,和pair差不多,都是存两个数据

7.map.size()表示map里面有多少个元素,也就是说在这个数列里面有多少个不同的数字

8.map.begin()表示起始元素,map.begin()->second表示起始元素存的数值,也就是某数字出现的次数,map.rbegin()表示map的最后一个元素

9.pair用.second,map用->second(大概是这样,以后发现不对的话再来修改)

相关推荐
代码雕刻家17 分钟前
数据结构-3.9.栈在递归中的应用
c语言·数据结构·算法
雨中rain17 分钟前
算法 | 位运算(哈希思想)
算法
slomay1 小时前
关于对比学习(简单整理
经验分享·深度学习·学习·机器学习
hengzhepa1 小时前
ElasticSearch备考 -- Async search
大数据·学习·elasticsearch·搜索引擎·es
Kalika0-02 小时前
猴子吃桃-C语言
c语言·开发语言·数据结构·算法
sp_fyf_20242 小时前
计算机前沿技术-人工智能算法-大语言模型-最新研究进展-2024-10-02
人工智能·神经网络·算法·计算机视觉·语言模型·自然语言处理·数据挖掘
小小洋洋3 小时前
BLE MESH学习1-基于沁恒CH582学习
学习
我是哈哈hh4 小时前
专题十_穷举vs暴搜vs深搜vs回溯vs剪枝_二叉树的深度优先搜索_算法专题详细总结
服务器·数据结构·c++·算法·机器学习·深度优先·剪枝
m0_689618284 小时前
水凝胶发生器,不对称设计妙,医电应用前景广
笔记
Ace'4 小时前
每日一题&&学习笔记
笔记·学习