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

相关推荐
wusixuan1310045 分钟前
最大闭合子图学习笔记 / P2805 [NOI2009] 植物大战僵尸
笔记·学习·算法·最大闭合子图
moxiaoran57535 分钟前
uni-app项目实战笔记5--使用grid进行定位布局
笔记·uni-app
羊小猪~~19 分钟前
数据库学习笔记(十五)--变量与定义条件与处理程序
数据库·人工智能·笔记·后端·sql·学习·mysql
梦境虽美,却不长25 分钟前
数据结构 线性表 学习 2025/6/12 21点27分
数据结构·学习
霸王蟹1 小时前
带你手写React中的useReducer函数。(底层实现)
前端·javascript·笔记·学习·react.js·typescript·前端框架
Humbunklung1 小时前
分布假设学习笔记
笔记·深度学习·学习
孟大本事要学习1 小时前
算法第15天:继续二叉树|前序递归+回溯与前序递归的场景总结、最大二叉树、合并二叉树、二叉搜索树中的搜索、验证二叉搜索树
算法
GalaxyPokemon1 小时前
LeetCode - 76. 最小覆盖子串
运维·服务器·数据结构·算法·leetcode
嵌入式@秋刀鱼1 小时前
《 第三章-招式初成》 C++修炼生涯笔记(基础篇)程序流程结构
linux·开发语言·数据结构·c++·笔记·visual studio code
HaiQinyanAN2 小时前
【学习笔记】重载和重写的注意事项
c++·笔记·学习