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 ...
随机推荐
- android Home键和返回键
在Android中,当按下Home键,默认情况下stop前台的actiity,即activity设置成onstop,而不是ondestory.如果再次启动该activity不是调用onCreate,而 ...
- unity打包模型存在的一个问题
http://blog.csdn.net/leonwei/article/details/39233747 发现U3D的模型打包可能存在一个bug,会导致发布到手机上的模型法线丢失(某些材质下变成全黑 ...
- 从输入 URL 到浏览器接收的过程中发生了什么事情?
从输入 URL 到浏览器接收的过程中发生了什么事情? What really happens when you navigate to a URL 上面两篇文章都解读的很好,值得阅读. 接下来在总结一 ...
- [BZOJ4016][FJOI2014]最短路径树问题
[BZOJ4016][FJOI2014]最短路径树问题 试题描述 给一个包含n个点,m条边的无向连通图.从顶点1出发,往其余所有点分别走一次并返回. 往某一个点走时,选择总长度最短的路径走.若有多条长 ...
- Tomcat无法启动问题
检查环境变量设置 再打开一个cmd, 1.输入if not exist "%JRE_HOME%\bin\java.exe" echo no jre java 回车 结果:no ...
- windows下的socket网络编程
windows下的socket网络编程 windows下的socket网络编程 clinet.c 客户端 server.c 服务器端 UDP通信的实现 代码如下 已经很久没有在windows下编程了, ...
- Python自动化之异常处理
1 异常基本结构 try: pass except Exception,ex: pass 2 异常种类 AttributeError 试图访问一个对象没有的树形,比如foo.x,但是foo没有属性x ...
- maven package
maven package test包下执行test 的配置文件 生成target目录,编译.测试代码,生成测试报告,生成jar/war文件 maven 配置文件详解 http://blog.csdn ...
- python判断文件和文件夹是否存在
import osos.path.isfile('test.txt') #如果不存在就返回Falseos.path.exists(directory) #如果目录或文件不存在就返回False
- ACM/ICPC 之 分治法入门(画图模拟:POJ 2083)
题意:大致就是要求画出这个有规律的Fractal图形了= = 例如 1 对应 X 2 对应 X X X X X 这个题是个理解分治法很典型的例子(详情请参见Code) 分治法:不断缩小规 ...