E. Magic Stones
题意:
给定两个数组,每次可以对一个数组选一个位置i($2 leq i leq n - 1$),让a[i]=a[i-1]+a[i+1]-a[i],或者b[i]=b[i-1]+b[i+1]-b[i]。问进行一些操作后,a和b能否相同。
分析:
考虑一次操作会变成什么样子。
a b c
a a+c-b c
我们发现这些数字差分后是不变的。于是对两个数组差分后,排序,看是否一样即可。注意判一下1和n是否相等。
代码:
#include<cstdio> #include<algorithm> #include<cstring> #include<iostream> #include<cmath> #include<cctype> #include<set> #include<queue> #include<vector> #include<map> using namespace std; typedef long long LL; inline int read() { int x=0,f=1;char ch=getchar();for(;!isdigit(ch);ch=getchar())if(ch=='-')f=-1; for(;isdigit(ch);ch=getchar())x=x*10+ch-'0';return x*f; } const int N = 100005; LL a[N], b[N], c1[N], c2[N]; int main() { int n = read(); for (int i = 1; i <= n; ++i) a[i] = read(); for (int i = 1; i <= n; ++i) b[i] = read(); if (a[1] != b[1] || a[n] != a[n]) { puts("No"); return 0; } for (int i = 1; i < n; ++i) c1[i] = a[i + 1] - a[i]; for (int i = 1; i < n; ++i) c2[i] = b[i + 1] - b[i]; sort(c1 + 1, c1 + n); sort(c2 + 1, c2 + n); for (int i = 1; i < n; ++i) { if (c1[i] != c2[i]) { puts("No"); return 0; } } puts("Yes"); return 0; }