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

相关推荐
好奇龙猫2 分钟前
【人工智能学习-AI入试相关题目练习-第九次】
人工智能·学习
zhangrelay28 分钟前
影响移动固态磁盘稳定性的原因有哪些呢?
笔记·学习
安特尼32 分钟前
X 推荐算法分析
算法·机器学习·推荐算法
棒棒的皮皮1 小时前
【深度学习】YOLO学习教程汇总
深度学习·学习·yolo·计算机视觉
詩不诉卿2 小时前
Zephyr学习之spi flash驱动记录(w25q128)
学习
罗湖老棍子2 小时前
强迫症冒险家的任务清单:字典序最小拓扑排序
数据结构·算法·图论·拓扑排序
lkbhua莱克瓦242 小时前
深入理解HTTP协议:从理论到SpringBoot实践
网络·笔记·后端·网络协议·http·javaweb
yanyu-yaya2 小时前
速学兼复习之vue3章节3
前端·javascript·vue.js·学习·前端框架
charlie1145141912 小时前
计算机图形学速通指南笔记(0)
c++·笔记·软件工程·计算机图形学·工程实践
不穿格子的程序员2 小时前
从零开始写算法——回溯篇4:分割回文串 + N皇后
算法·深度优先·dfs