• Codeforces Round #371 (Div. 2)(setunique)


    B. Filya and Homework
    time limit per test
    1 second
    memory limit per test
    256 megabytes
    input
    standard input
    output
    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.

    Input

    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.

    Output

    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).

    Examples
    Input
    5
    1 3 3 2 1
    Output
    YES
    Input
    5
    1 2 3 4 5
    Output
    NO
    Note

    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去重;

     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 
     4 set<int>st;
     5 
     6 int main(void)
     7 {
     8     std::ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
     9     int n;
    10     cin >> n;
    11     for(int i=0; i<n; i++)
    12     {
    13         int x;
    14         cin >> x;
    15         st.insert(x);
    16     }
    17     int len=st.size();
    18     if(len<3)
    19     {
    20         cout << "YES" << endl;
    21     }
    22     else if(len==3)
    23     {
    24         set<int>::iterator it;
    25         it=st.end();
    26         if(*st.begin()+(*(--it))==(*(--it))*2)
    27         {
    28             cout << "YES" << endl;
    29         }
    30         else
    31         {
    32             cout << "NO" << endl;
    33         }
    34     }
    35     else
    36     {
    37         cout << "NO" << endl;
    38     }
    39 
    40   fflush(stdout);                  //*****fflush(stdout)的作用是清除stdout输出域缓存,加快显示输出结果的速度;
    41     return 0;
    42 }

     

    2:用unique去重;

     1 #include <bits/stdc++.h>
     2 #define MAXN 100000+10
     3 using namespace std;
     4 
     5 int a[MAXN];
     6 
     7 int main(void)
     8 {
     9     std::ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    10     int n;
    11     cin >> n;
    12     for(int i=0; i<n; i++)
    13     {
    14         cin >> a[i];
    15     }
    16     sort(a, a+n);
    17     int len = unique(a, a+n) - a;
    18     if(len<3)
    19     {
    20         cout << "YES" << endl;
    21     }
    22     else if(len==3)
    23     {
    24         if(a[2]+a[0]==2*a[1])
    25         {
    26             cout << "YES" << endl;
    27         }
    28         else
    29         {
    30             cout << "NO" << endl;
    31         }
    32     }
    33     else
    34     {
    35         cout << "NO" << endl;
    36     }
    37     fflush(stdout);            // *****是清除stdout输出域缓存,加快显示输出结果的速度;
    38     return 0;
    39 }



    http://www.cnblogs.com/heyonggang/p/3243477.html

    unique的用法

  • 相关阅读:
    干掉你的老板(小游戏)
    SEO优化数据系列表(图)
    javascript动态加载三
    javascript动态加载二
    截屏
    vimdiff
    pscp scp ftp samba windows send files to linux
    login windows 10 with passwd instead of pin
    modify requirements.txt
    整片注释 ,shell
  • 原文地址:https://www.cnblogs.com/geloutingyu/p/5872206.html
Copyright © 2020-2023  润新知