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(大概是这样,以后发现不对的话再来修改)

相关推荐
aini_lovee2 分钟前
MATLAB圆锥滚子轴承滚子参数分析程序
人工智能·算法·matlab
_olone7 分钟前
牛客每日一题:显生之宙(Java)
java·开发语言·算法·牛客
左左右右左右摇晃9 分钟前
红黑树笔记整理
笔记
嫂子开门我是_我哥21 分钟前
心电域泛化研究从0入门系列 | 第二篇:心电信号预处理全攻略——扫清域泛化建模的第一道障碍
人工智能·算法·ecg
小光学长25 分钟前
基于ssm的膳食健康管理系统e6whl4q7(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。
java·开发语言·数据库·学习·ssm
wefg11 小时前
【算法】算数基本定理、分解质因数
算法
j_xxx404_1 小时前
力扣困难算法精解:串联所有单词的子串与最小覆盖子串
java·开发语言·c++·算法·leetcode·哈希算法
挠头猴子1 小时前
一个数组去重,两个数组找不同或相同
数据结构·算法
big_rabbit05021 小时前
[算法][力扣167]Two Sum II
算法·leetcode·职场和发展
颜酱1 小时前
二分图核心原理与判定算法
javascript·后端·算法