天天看點

B. Filya and Homework

​​http://codeforces.com/contest/714/problem/B​​

給定一個序列,對于每一個元素,隻能 + 或者 - 一個數val。這個數一旦標明,就不能改。

問能否變成全部數字都一樣。

一開始還以為沒 + 一次,就要 - 一次。 結果不是,一直wa

那麼這樣的話,這題的正解是觀察法。也可以證明。

①、全部數字都一樣、有兩個不同數字、三個不同數字(a[1] + a[3] =  2 * a[2])這些都易懂

那4個不同數字為什麼是no呢

可以想象成找不到一個點,作為圓心,包含另外3個點(這3點在一直線)。

是以對于有三個不同數字那個,其實就是判斷是否為圓心。

B. Filya and Homework
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL;

#include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
const int maxn = 100000 + 20;
int a[maxn];
int n;
LL val;
set<int> ss;
void work() {
    cin >> n;
    LL sum = 0;
    for (int i = 1; i <= n; ++i) {
        scanf("%d", &a[i]);
        sum += a[i];
        ss.insert(a[i]);
    }
//    sort(a + 1, a + 1 + n);
    if (ss.size() == 1 || ss.size() == 2) {
        printf("YES\n");
        return ;
    } else if (ss.size() >= 4) {
        printf("NO\n");
        return ;
    } else {
        int now = 0;
        for (set<int> :: iterator it = ss.begin(); it != ss.end(); ++it) {
            a[++now] = *it;
        }
        if (a[1] + a[3] == 2 * a[2]) {
            printf("YES\n");
            return ;
        }
        printf("NO\n");
    }
}

int main() {
#ifdef local
    freopen("data.txt","r",stdin);
#endif
    work();
    return 0;
}