CF1110E Magic Stones 差分

传送门


将原数组差分一下,设\(d_i = c_{i+1} - c_i\)

考虑在\(i\)位置的一次操作会如何影响差分数组

\(d_{i+1}' = c_{i+1} - (c_{i+1} + c_{i-1} - c_i) = c_i - c_{i-1} = d_i\)

\(d_i' = (c_{i+1} + c_{i-1} - c_i) - c_{i-1} = c_{i+1} - c_i = d_{i+1}\)

所以本质上一次操作交换了差分数组中的两个元素

所以只需要判断差分数组中的每一个元素能否对应即可。注意还需要判断\(c_1=t_1\)

#include<bits/stdc++.h>
using namespace std;


inline int read(){
    int a = 0;
    char c = getchar();
    bool f = 0;
    while(!isdigit(c)){
        if(c == '-')
            f = 1;
        c = getchar();
    }
    while(isdigit(c)){
        a = (a << 3) + (a << 1) + (c ^ '0');
        c = getchar();
    }
    return f ? -a : a;
}

const int MAXN = 1e5 + 7;
int c[MAXN] , t[MAXN] , N; 

int main(){
    N = read();
    for(int i = 1 ; i <= N ; ++i)
        c[i] = read();
    for(int i = 1 ; i <= N ; ++i)
        t[i] = read();
    if(c[1] != t[1]){
        puts("No");
        return 0;
    }
    for(int i = 1 ; i < N ; ++i){
        c[i] = c[i + 1] - c[i];
        t[i] = t[i + 1] - t[i];
    }
    sort(c + 1 , c + N);
    sort(t + 1 , t + N);
    for(int i = 1 ; i < N ; ++i)
        if(c[i] != t[i]){
            puts("No");
            return 0;
        }
    puts("Yes");
    return 0;
}
posted @ 2019-02-08 16:19  cjoier_Itst  阅读(279)  评论(0编辑  收藏  举报