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 ...
随机推荐
- BZOJ4034——[HAOI2015]T2
1.题目大意:用一个数据结构支持树的点修改和子树修改.树上路径和 2.分析:树链剖分裸题 #include <cstdio> #include <cstdlib> #inclu ...
- Android沉浸式任务栏的实现
1.MainActivity.java public class MainActivity extends Activity { @Override protected void onCreate(B ...
- struts2将servlet对象注入到Action中
在struts2框架中,可以通过IoC方式将servlet对象注入到Action中,通常需要Action实现以下接口: a. ServletRequestAware: 实现该接口的Action可以直接 ...
- scp和rsync限制传输速度
1.scp 限速100KB/s scp -l 1000 test root@192.168.1.104 此时的传输速率就是1M/8=100KB左右 2.rsync 限速100KB/s rsync -a ...
- 仿QQ侧滑菜单<大自然的搬运工-代码不是我的>
1.记录下效果图 2.二个工具类 package myapplication.com.myapplicationfortest.utils; import android.util.Log; /** ...
- C语言宏定义时#(井号)和##(双井号)的用法1
#在英语里面叫做 pound 在C语言的宏定义中,一个#表示字符串化:两个#代表concatenate 举例如下: #include <iostream> void quit_comman ...
- java实现远程储存读取文件
import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.File;import java.io.FileN ...
- JAVA调用动态链接库DLL之JNative学习
package com.ehfscliax; import java.io.UnsupportedEncodingException;import java.net.URLEncoder;import ...
- 第三方登录(QQ登录)开发流程详解
原文:http://www.cnblogs.com/it-cen/p/4338202.html 近排由于工作的繁忙,已经一个星期没写博文做分享了,接下来我对网站接入第三方登录----QQ登录的实现逻辑 ...
- POJ 1458 1159
http://poj.org/problem?id=1458 一道容易的DP,求最长公共子序列的 dp[i][j],代表str1中的第i个字母和str2中的第j个字母的最多的公共字母数 #includ ...