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

相关推荐
知识分享小能手8 小时前
Ubuntu入门学习教程,从入门到精通, Ubuntu 22.04中的进程管理详解(15)
linux·学习·ubuntu
Timmylyx05188 小时前
2025年最后一搏—— Educational Codeforces Round 186 (Rated for Div. 2) 题解
算法·codeforces·比赛日记
0和1的舞者8 小时前
SpringAOP详解(二)
学习·spring·切面·代理·知识·springaop
微光闪现8 小时前
国际航班动态提醒与延误预测优选平台指南
大数据·人工智能·算法
leoufung8 小时前
LeetCode 120. Triangle:从 0 分到 100 分的思考过程(含二维 DP 与空间优化)
linux·算法·leetcode
iiiiii118 小时前
TD(λ),资格迹(Eligibility Traces)与时序差分学习的统一
人工智能·学习·机器学习·强化学习·rl
航Hang*9 小时前
Photoshop 图形与图像处理技术——第6章:通道与蒙版的应用
图像处理·笔记·ui·photoshop
gihigo19989 小时前
基于反步法的路径追踪控制
算法
Jim-2ha09 小时前
【JavaScript】常见排序算法实现
javascript·算法·排序算法
走在路上的菜鸟9 小时前
Android学Flutter学习笔记 第一节 Android视角认知Flutter(View,intent,Async UI)
android·学习·flutter