Codeforces Round #371 (Div. 2)(set\unique)
1 second
256 megabytes
standard input
standard output
Today, hedgehog Filya went to school for the very first time! Teacher gave him a homework which Filya was unable to complete without your help.
Filya is given an array of non-negative integers a1, a2, ..., an. First, he pick an integer x and then he adds x to some elements of the array (no more than once), subtract x from some other elements (also, no more than once) and do no change other elements. He wants all elements of the array to be equal.
Now he wonders if it's possible to pick such integer x and change some elements of the array using this x in order to make all elements equal.
The first line of the input contains an integer n (1 ≤ n ≤ 100 000) — the number of integers in the Filya's array. The second line contains n integers a1, a2, ..., an (0 ≤ ai ≤ 109) — elements of the array.
If it's impossible to make all elements of the array equal using the process given in the problem statement, then print "NO" (without quotes) in the only line of the output. Otherwise print "YES" (without quotes).
5
1 3 3 2 1
YES
5
1 2 3 4 5
NO
In the first sample Filya should select x = 1, then add it to the first and the last elements of the array and subtract from the second and the third elements
题意:给出一个n个元素的数组a,是否存在这样一个数k,使得a中一些元素加上k,a中一些元素减去k后a中所有元素都相等;
思路:如果有重复的元素,我们只需要判断其中一个元素就可以了,所有可以先去重;
去重后如果剩余元素为1个,则不需要加减k,一定可行;
如果剩余元素为2个,假设为a[0], a[1],只要给值为a[0]的元素加上值为a[1]-a[0]的k, 或者给值为a[1]的元素加上值为a[0]-a[1]的k即可;
如果剩余元素为3个,假设为排序后为a[0], a[1], a[2],那么如果a[0]+a[2]==2*a[1]则可行,否则不可行;
如果剩余元素个数大于3,则一定不可行,例如去重且排序后后为a[0],a[1],a[2],a[3],a[4];无论取其中哪个元素作为目标值,它的左边或者右边一定有多个元素,要将这多个元素值变为和它一样一定要加上或者减去不同的数才可能实现;假如我们取a[0]为目标值,a[1]要减去k1=a[1]-a[0],a[2]要减去k2=a[2]-a[0],a[3]要减去k3=a[3]-a[0],a[4]要减去k4=a[4]-a[0];又因为a[1], a[2], a[3], a[4]都不相等,所以k1, k2, k3, k4 值也不同;取a[1], a[2], a[3], a[4]为目标值我们也可以通过这样的推理得到同样的结果;
由此,我们证得了如果去重后的元素个数大于3,则一定不可行;
代码:
1:用set去重;
#include <bits/stdc++.h>
using namespace std; set<int>st; int main(void)
{
std::ios::sync_with_stdio(false), cin.tie(), cout.tie();
int n;
cin >> n;
for(int i=; i<n; i++)
{
int x;
cin >> x;
st.insert(x);
}
int len=st.size();
if(len<)
{
cout << "YES" << endl;
}
else if(len==)
{
set<int>::iterator it;
it=st.end();
if(*st.begin()+(*(--it))==(*(--it))*)
{
cout << "YES" << endl;
}
else
{
cout << "NO" << endl;
}
}
else
{
cout << "NO" << endl;
} fflush(stdout); //*****fflush(stdout)的作用是清除stdout输出域缓存,加快显示输出结果的速度;
return ;
}
2:用unique去重;
#include <bits/stdc++.h>
#define MAXN 100000+10
using namespace std; int a[MAXN]; int main(void)
{
std::ios::sync_with_stdio(false), cin.tie(), cout.tie();
int n;
cin >> n;
for(int i=; i<n; i++)
{
cin >> a[i];
}
sort(a, a+n);
int len = unique(a, a+n) - a;
if(len<)
{
cout << "YES" << endl;
}
else if(len==)
{
if(a[]+a[]==*a[])
{
cout << "YES" << endl;
}
else
{
cout << "NO" << endl;
}
}
else
{
cout << "NO" << endl;
}
fflush(stdout); // *****是清除stdout输出域缓存,加快显示输出结果的速度;
return ;
}
http://www.cnblogs.com/heyonggang/p/3243477.html
unique的用法
Codeforces Round #371 (Div. 2)(set\unique)的更多相关文章
- Codeforces Round #371 (Div. 1)
A: 题目大意: 在一个multiset中要求支持3种操作: 1.增加一个数 2.删去一个数 3.给出一个01序列,问multiset中有多少这样的数,把它的十进制表示中的奇数改成1,偶数改成0后和给 ...
- Codeforces Round #371 (Div. 2)B. Filya and Homework
题目链接:http://codeforces.com/problemset/problem/714/B 题目大意: 第一行输入一个n,第二行输入n个数,求是否能找出一个数x,使得n个数中的部分数加上x ...
- Codeforces Round #371 (Div. 2) B. Filya and Homework 水题
B. Filya and Homework 题目连接: http://codeforces.com/contest/714/problem/B Description Today, hedgehog ...
- Codeforces Round #371 (Div. 2)E. Sonya and Problem Wihtout a Legend[DP 离散化 LIS相关]
E. Sonya and Problem Wihtout a Legend time limit per test 5 seconds memory limit per test 256 megaby ...
- Codeforces Round #371 (Div. 2) - B
题目链接:http://codeforces.com/contest/714/problem/B 题意:给定一个长度为N的初始序列,然后问是否能找到一个值x,然后使得序列的每个元素+x/-x/不变,最 ...
- Codeforces Round #371 (Div. 2) - A
题目链接:http://codeforces.com/contest/714/problem/A 题意:有两个人A,B 给定A的时间区间[L1,R1], B的时间区间[L2,R2],然后在正好K分钟的 ...
- 严格递增类的dp Codeforces Round #371 (Div. 1) C dp
http://codeforces.com/contest/713 题目大意:给你一个长度为n的数组,每次有+1和-1操作,在该操作下把该数组变成严格递增所需要的最小修改值是多少 思路:遇到这类题型, ...
- Codeforces Round #371 (Div. 2) C 大模拟
http://codeforces.com/contest/714/problem/C 题目大意:有t个询问,每个询问有三种操作 ①加入一个数值为a[i]的数字 ②消除一个数值为a[i]的数字 ③给一 ...
- Codeforces Round #371 (Div. 1) C. Sonya and Problem Wihtout a Legend 贪心
C. Sonya and Problem Wihtout a Legend 题目连接: http://codeforces.com/contest/713/problem/C Description ...
随机推荐
- error C2504 类的多层继承 头文件包含
error C2504:头文件包含不全 今天碰到了很烦的问题,继承一个类之后,感觉头文件都包含了,可还是出现父类未定义的问题,最后发现,子类的子类在实现时,需要在cpp文件中包含所有他的父类的定义.因 ...
- 用 FragmentManager 替换时使用 GoogleMaps 崩溃 app
我要用下面的代码来加载的片段的项已被选定在 NavigationDrawer,然而 MapFragment 包含它里面的谷歌地图片段和它崩溃后试图打开它第二次. 这里是我使用的代码: @Overrid ...
- ubuntu安装python一些安装包
sudo apt-get install python-pip sudo pip install distribute sudo pip install nose sudo pip install v ...
- JavaScript的作用域与作用域链
作用域 作用域就是变量与函数的可访问范围,即作用域控制着变量与函数的可见性和生命周期.可以说,变量和函数在什么时候可以用,什么时候被摧毁,这都与作用域有关. JavaScript中,变量的作用域有全局 ...
- 贴一下WC总结里提到的那道裸题吧。。。
[bzoj4034][HAOI2015]T2 试题描述 有一棵点数为 N 的树,以点 1 为根,且树点有边权.然后有 M 个 操作,分为三种: 操作 1 :把某个节点 x 的点权增加 a . 操作 2 ...
- BZOJ 2412: 电路检修
Description [0,x]中全是1,其余全是0,每个点有一个权值,求最坏情况下得到x的最小权值. Sol DP+单调队列. 你可以去看我的这篇Blog...开这篇纯属为了骗访问... http ...
- Javaweb Servlet出现Class xxx is not a servlet错误原因
- 解决Python往MySQL插入中文时报错的问题
今天遇到一个问题,用Python往MySQL插入数据时,若数据中包含中文会报类似下面的错误: ERROR 1366: Incorrect string value: '\xE4\xB8\xAD\xE5 ...
- Oracle占用8080端口问题的解决
可能在本地同时安装过Tomcat和Oracle的人都会知道,安装完Oracle后,会发现Tomcat的8080端口已经被Oracle占用了. 完全安装Oracle数据库后,当我们访问8080端口时,会 ...
- zaqar项目介绍
Zaqar is a multi-tenant cloud messaging and notification service for web and mobile developers. It c ...