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 ...
随机推荐
- [转]看了这个才发现jQuery源代码不是那么晦涩
很多人觉得jquery.ext等一些开源js源代码 十分的晦涩,读不懂,遇到问题需要调试也很费劲.其实我个人感觉主要是有几个方面的原因: 对一些js不常用的语法.操作符不熟悉 某个function中又 ...
- Sublime多行编辑快捷键
鼠标选中多行,按下 Ctrl Shift L (Command Shift L) 即可同时编辑这些行: 鼠标选中文本,反复按 CTRL D (Command D) 即可继续向下同时选中下一个相同的文本 ...
- Java Io(数据输入输出流)
Java Io 字节流中的DataInputStream 和 DataOutputStream,使用流更加方便,是流的一个扩展,更方便读取int, long,字符等类型数据. 事例代码如下: pack ...
- Codeforces 260 A - A. Laptops
题目链接:http://codeforces.com/contest/456/problem/A 解题报告:有n种电脑,给出每台电脑的价格和质量,要你判断出有没有一种电脑的价格小于另一种电脑但质量却大 ...
- OSI参考模型与TCP/IP协议模型
OSI和TCP/IP都是为了计算机之间更好的互联的. 计算机网络是一个复杂的系统,比如两台计算机进行通信不仅仅只是有一条通信线就可以了. 还有很多的工作需要完成,例如: 如何知道对方计算机是否做好准备 ...
- 强制QQ好友
tencent://AddContact/?fromId=45&fromSubId=1&subcmd=all&uin=32595667&website=www.oicq ...
- How do I get ASP.NET Web API to return JSON instead of XML using Chrome
public static class WebApiConfig { public static void Register(HttpConfiguration config) { config.Ro ...
- POJ 1062 ( dijkstra )
http://poj.org/problem?id=1062 一个中文题,一个多月之前我做过,当时我是用搜索写的,不过苦于卡在无法确定等级关系,所以就错了. 看了别人的博客后,我还是不是很理解所谓的枚 ...
- kendoUI grid 过滤时出错:TypeError toLowerCase is not a function
错误原因:类型不一致. 有些过滤类型为字符串,有些为整型时.
- C++构造函数初始化顺序
[本文链接] http://www.cnblogs.com/hellogiser/p/constructor-order.html 1.构造函数.析构函数与拷贝构造函数介绍 构造函数 构造函数不能有返 ...