E. Magic Stones CF 思維題
Grigory has nn magic stones, conveniently numbered from 11 to nn . The charge of the ii -th stone is equal to cici .
Sometimes Grigory gets bored and selects some inner stone (that is, some stone with index
Andrew, Grigory‘s friend, also has n
The first line contains one integer
The second line contains integers c1,c2,…,cnc1,c2,…,cn (0≤ci≤2?1090≤ci≤2?109 ) — the charges of Grigory‘s stones.
The second line contains integers t1,t2,…,tnt1,t2,…,tn (0≤ti≤2?1090≤ti≤2?109 ) — the charges of Andrew‘s stones.
OutputIf there exists a (possibly empty) sequence of synchronization operations, which changes all charges to the required ones, print "Yes".
Otherwise, print "No".
Examples Input Copy4 7 2 4 12 7 15 10 12Output Copy
YesInput Copy
3 4 4 4 1 2 3Output Copy
NoNote
In the first example, we can perform the following synchronizations (11 -indexed):
- First, synchronize the third stone [7,2,4,12]→[7,2,10,12][7,2,4,12]→[7,2,10,12] .
- Then synchronize the second stone: [7,2,10,12]→[7,15,10,12][7,2,10,12]→[7,15,10,12] .
In the second example, any operation with the second stone will not change its charge.
思路:
註意看題目給出的公式
c[i]‘=c[i+1]+c[i-1]-c[i]
可以變形成
c[i+1]-c[i]‘=c[i]-c[i-1]
c[i]‘-c[i-1]=c[i+1]-c[i]
這樣看來就是將第i項左右差分交換,可以把這個看成一種排序
所以將差分全部重新排序之後,如果上下差分都相同,則滿足題目要求
#include <iostream> #include <stdio.h> #include <stdlib.h> #include <algorithm> using namespace std; typedef long long ll; const int maxn=1e5+10; ll a[maxn],b[maxn],c[maxn],d[maxn]; int main() { int n; cin>>n; for(int i=1;i<=n;i++) scanf("%I64d",&a[i]); for(int i=1;i<=n;i++) scanf("%I64d",&b[i]); for(int i=1;i<n;i++) { c[i]=a[i+1]-a[i]; d[i]=b[i+1]-b[i]; } sort(c+1,c+n); sort(d+1,d+n); if(a[1]!=b[1]||a[n]!=b[n]) { printf("No\n"); return 0; } for(int i=1;i<n;i++) { if(c[i]!=d[i]) { printf("No\n"); return 0; } } printf("Yes\n"); return 0; }
E. Magic Stones CF 思維題