设f[i]为选择i对i-1和i+1所带来的贡献。则有f[i-1]+f[i+1]+a[i]-2f[i]=b[i],特殊地,f[2]+a[1]=b[1],f[n-1]+a[n]-2f[n]=b[n]。可以发现这样我们有n-1个未知数和n个方程,代入求解判断是否矛盾即可。
但这只有必要性显然,为什么是充分的呢?我也不知道。还是前缀和的做法比较简单易懂。
#include<iostream> #include<cstdio> #include<cmath> #include<cstdlib> #include<cstring> #include<algorithm> using namespace std; #define ll long long #define N 100010 char getc(){char c=getchar();while ((c<'A'||c>'Z')&&(c<'a'||c>'z')&&(c<'0'||c>'9')) c=getchar();return c;} int gcd(int n,int m){return m==0?n:gcd(m,n%m);} int read() { int x=0,f=1;char c=getchar(); while (c<'0'||c>'9') {if (c=='-') f=-1;c=getchar();} while (c>='0'&&c<='9') x=(x<<1)+(x<<3)+(c^48),c=getchar(); return x*f; } int T,n,a[N],b[N]; ll f[N]; int main() { #ifndef ONLINE_JUDGE freopen("bzoj5071.in","r",stdin); freopen("bzoj5071.out","w",stdout); const char LL[]="%I64d "; #else const char LL[]="%lld "; #endif T=read(); while (T--) { n=read(); for (int i=1;i<=n;i++) a[i]=read(); for (int i=1;i<=n;i++) b[i]=read(); f[2]=b[1]-a[1]; for (int i=3;i<=n;i++) f[i]=2*f[i-1]+b[i-1]-a[i-1]-f[i-2]; if (f[n-1]+a[n]-b[n]!=2*f[n]) cout<<"NO "; else cout<<"YES "; } return 0; }