博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ZOJ Monthly, November 2012 - G - Gao The Sequence
阅读量:4360 次
发布时间:2019-06-07

本文共 2087 字,大约阅读时间需要 6 分钟。

Gao The Sequence

Time Limit: 2 Seconds     
 
Memory Limit: 65536 KB

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 sequenceB1,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

33 24 25 232 07 13 1

Sample Output

YESNO

分析:将原题转化为直接在差数组上操作。利用贪心的思路,若目前节余的值t+s[i-1]仍然<c[i],则NO。一直没有NO就是YES。

#include
#include
typedef long long u64;u64 a[10010], b[10010], c[10010], s[10010], t;int n;int main() { int i; while (scanf("%d", &n) != EOF) { s[0] = 0; t = 0; for (i = 1; i <= n; ++i) { scanf("%lld%lld", &a[i], &b[i]); c[i] = a[i] - b[i]; s[i] = s[i - 1] + c[i]; } if (s[n]&1) { printf("NO\n"); continue; } for (i = n; i >= 1; --i) { if (s[i - 1] + t < c[i]) { printf("NO\n"); goto L; } if (s[i - 1] - c[i] <= t) { break; } t += c[i]; } printf("YES\n");L: ; } return 0;}

转载于:https://www.cnblogs.com/baidongtan/archive/2012/11/30/2796695.html

你可能感兴趣的文章
使用ROW_NUMBER()查询:列名 'RowNumber' 无效。
查看>>
.2-Vue源码起步(2)
查看>>
java中比较字符串方法
查看>>
CSS3选择器:nth-child和:nth-of-type之间的差异
查看>>
单循环链表的表示和实现
查看>>
python数据类型:字符串
查看>>
为什么你应该先成为全栈工程师
查看>>
清除浮动
查看>>
在HTML中使用JavaScript需要注意的问题
查看>>
OSError: libcudart.so.7.5: cannot open shared object file: No such file or directory
查看>>
LFS中各程序包的作用
查看>>
妙味课堂作业20160113(优化版)
查看>>
bzoj4653 [Noi2016]区间
查看>>
cogs896 圈奶牛(凸包)
查看>>
C#中对话框的使用(比较全)
查看>>
5月11日 python学习总结 子查询、pymysql模块增删改查、防止sql注入问题
查看>>
sqlserver查找使用了某个字段的所有存储过程
查看>>
TCP/IP协议族(三) 数字签名与HTTPS详解
查看>>
bitset里面一些函数的用法
查看>>
《我曾》火了:人这辈子,最怕突然听懂这首歌
查看>>