HRZ的序列-----附STL set常用用法

在这里插入图片描述
在这里插入图片描述

Sample Input

2
5
1 2 3 4 5
5
1 2 3 4 5

Sample Output

NO
NO

解题思路以及关键代码

关键点 注意数据范围 要用 long long
把元素都insert到set中
①如果集合元素小于等于2个的话,显然YES;
②如果集合元素大于等于4个的话,显然NO;
③集合元素等于3个的话
排序一下 然后对 相对小 和 相对大的数值进行 + / -
然后重新insert进集合中,如果集合元素个数为1的话,就YES,反之NO。

全部代码

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<set>
using namespace std;
const int maxn = 10005;
set<long long> s;
long long a[maxn];
int t, n;
int main()
{
	scanf("%d", &t);
	while (t--)
	{
		s.clear();
		scanf("%d", &n);
		long long tmp;
		for (int i = 0; i < n; i++)
		{
			scanf("%lld", &tmp);
			s.insert(tmp);
			a[i] = tmp;
		}
		if (s.size() <= 2)
			cout << "YES" << endl;
		else if (s.size() > 3)
			cout << "NO" << endl;
		else
		{
			sort(a, a + n);
			long long c = (a[n - 1] - a[0]) / 2;  //数据保证k为整数 
			long long ans = a[n - 1] - c;
			for (int i = 0; i < n; i++)
			{
				if (a[i] < ans) a[i] += c;
				if (a[i] > ans) a[i] -= c;
			}
			s.clear();
			for (int i = 0; i < n; i++)
			{
				s.insert(a[i]);
			}
			if (s.size() == 1)
				cout << "YES" << endl;
			else
				cout << "NO" << endl;
		}
	}
	return 0;
}

Set的常用用法

//声明
set<int> s;  
//赋值
s.insert(3);  s.insert(1); s.insert(2); s.insert(2);  set默认是升序排序 
for(auto x:s)
	cout<<x;  遍历输出的是 1 2 3
//大小
s.size();
//查找
if(s.find(1)!=s.end()) 
	cout<<"the element is in the set";
//遍历
for(set<int>::iterator it = s.begin();it!=s.end();it++)
	cout<<*it;
for(auto &x:s)
	cout<<x;
//删除和清空
s.erase(value);
s.clear();
//二分
set<int>:: iterator it = s.lower_bound(5);查找一个大于等于5的元素,返回指针
if(it!=end()) //如果存在这样的元素
	cout<<*it;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值