ZOJ3672:Gao The Sequence

2014-11-24 12:50:37 · 作者: · 浏览: 1

You are given a sequence of integers, A1,A2,...,An. And you are allowed a manipulation on the sequence to transform the origin sequence into another sequence B1,B2,...,Bn(Maybe the two sequences are same ). The manipulation is specified as the following three steps:

1.Select an integer Ai and choose an arbitrary positive integer delta as you like.

2.Select some integers Aj satisfying j < i, let's suppose the selected integers are Ak1,Ak2,...,Akt , then subtract an arbitrary positive integer Di from Aki (1 ≤ i ≤ t) as long as sum(Di) = delta.

3.Subtract delta from Ai.

The manipulation can be performed any times. Can you find a way to transform A1,A2,...,An to B1,B2,...,Bn

Input

The input consist of multiple cases. Cases are about 100 or so. For each case, the first line contains an integer N(1 ≤ N ≤ 10000) indicating the number of the sequence. Then followed by N lines, ith line contains two integers Ai and Bi (0 ≤ Bi ≤ Ai ≤ 4294967296).

Output

Output a single line per case. Print "YES" if there is a certain way to transform Sequence A into Sequence B. Print "NO" if not.

Sample Input

3
3 2
4 2
5 2
3
2 0
7 1
3 1

Sample Output

YES
NO

题意:
两个序列a,b,问a能不能经过一定的操作变成b
操作规定如下:
在a中任选一个a[i],减去一个数k,然后a[1]~a[i-1]中任意一个数减去任意值,其减去的数的和要等于k
经过n次操作后,a是否能编程b

思路:
1.因为每一次操作总共减去两个k,所以增量之和sum一定要是偶数
2.如果存在2*(a[i]-b[i])>sum,也是不行的
因为这种情况下,a数组要减去2*(a[i]-b[i]),其中a[i]要减去(a[i]-b[i]),其他数也要减去和为(a[i]-b[i])的量,但明显增量不足,所以不行

#include 
   
    
#include 
    
      #include 
     
       using namespace std; long long sum,maxn,tem,a,b; int main() { int n,i,j; while(~scanf("%d",&n)) { sum = maxn = tem = 0; for(i = 1; i<=n; i++) { scanf("%lld%lld",&a,&b); tem=a-b; sum+=tem; maxn = (maxn,tem); } if(sum&1 || 2*maxn>sum) printf("NO\n"); else printf("YES\n"); } return 0; }