zoj 3672 Gao The Sequence
来源:互联网 发布:python可以机器人 编辑:程序博客网 时间:2024/04/19 01:59
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4915
题目大意:
给定一个数组A[],经以下操作得到B[].
1)从A[]任选1个数Ai,和任意定义一个正数D.
2)任选Aj(j<i),减去一个数dj,使得sum(dj)(所有减去的数之和)= D.
3)Ai减去D.
现在给出A[]和B[],判断是否能通过以上操作由A[]得B[].
数组长度N(1 ≤ N ≤ 10000),0 ≤ Bi ≤ Ai ≤ 4294967296.
题目思路:
一开始想到只要满足对于所有i,满足Ai-Bi<=sum(A1-B1+A2-B2+...+An-Bn)-(Ai-Bi),就能由A[]得B[],不过很容易找到反例,不过这条路已经在通往正确的路上了,可惜比赛时又想到贪心上去了,结果。。。。。。。
题外话多说无益,小小抱怨一下,还是自己练习不够上心啊。。
虽然Ai-Bi<=A1-B1+A2-B2+...+An-Bn-(Ai-Bi)这不是充要条件,但是这是必要条件,只要是YES的数据,那么必然满足这个式子,为了方便我们设Ci = Ai-Bi,这样我们就是判断能否通过题目的操作使得C[]能全变为0。
通过必要条件,我们很容易得知,使得Ci变为0可以和任意一个除它自身之外的数cj有关,无所谓j<i或j>i,
既然这样我们就取一个数Ci,我们又能退出这样一个必要条件Ci%2 == (sum(ci~n)-MAX)%2,这个的话自己分析分析,很容易证明的。
当然同时满足这两个条件还是不足以判断是YES的,要消去Ci,也就是说s除了Ci以外的C[]数组要能通过描述的操作使得和为Ci,如果可行的话,那么必然是YES,证明也是简单的,可以自我尝试。
代码:
#include <stdlib.h>#include <string.h>#include <stdio.h>#include <ctype.h>#include <math.h>#include <stack>#include <queue>#include <map>#include <set>#include <vector>#include <string>#include <iostream>#include <algorithm>using namespace std;#define ll long long#define ls rt<<1#define rs ls|1#define lson l,mid,ls#define rson mid+1,r,rs#define middle (l+r)>>1#define clr_all(x,c) memset(x,c,sizeof(x))#define clr(x,c,n) memset(x,c,sizeof(x[0])*(n+1))#define eps (1e-8)#define MOD 1000000007#define INF 0x3f3f3f3f#define PI (acos(-1.0))#pragma comment(linker, "/STACK:102400000,102400000")template <class T> T _max(T x,T y){return x>y? x:y;}template <class T> T _min(T x,T y){return x<y? x:y;}template <class T> T _abs(T x){return (x < 0)? -x:x;}template <class T> T _mod(T x,T y){return (x > 0)? x%y:((x%y)+y)%y;}template <class T> void _swap(T &x,T &y){T t=x;x=y;y=t;}template <class T> void getmax(T& x,T y){x=(y > x)? y:x;}template <class T> void getmin(T& x,T y){x=(x<0 || y<x)? y:x;}int TS,cas=1;const int M=10000+5;int n;ll c[M],sum;void run(){int i,j;sum=0;for(i=1;i<=n;i++){ll a,b;scanf("%lld%lld",&a,&b);c[i]=a-b;sum+=c[i];}if(n==1){printf("%s\n",sum? "NO":"YES");return;}if(n==2){printf("%s\n",c[1]!=c[2]? "NO":"YES");return;}sum-=c[n];bool flag=false;if(c[n]<=sum && c[n]%2==sum%2){for(i=1;i<=n-2;i++) c[n-1]-=c[i];if(c[n-1] <= c[n]) flag=true;}printf("%s\n",flag? "YES":"NO");}void preSof(){}int main(){ //freopen("input.txt","r",stdin); //freopen("output.txt","w",stdout); preSof(); //run(); while((~scanf("%d",&n))) run(); //for(scanf("%d",&TS);cas<=TS;cas++) run(); return 0;}
- zoj 3672 Gao The Sequence
- zoj - 3672 - Gao The Sequence
- ZOJ 3672 Gao The Sequence
- ZOJ3672:Gao The Sequence
- ZOJ 3647 Gao the Grid
- ZOJ 3647 Gao the Grid
- zoj 3647 Gao the Grid
- ZOJ-3647 Gao the Grid
- ZOJ 3647 Gao the Grid
- ZOJ 3535 Gao the String II
- ZOJ 3648 Gao the Grid II
- Zoj 3647 Gao the Grid (数论?)
- zoj 3535 Gao the String II(ac自动机+dp)
- zoj 3533 Gao the String I(伸展树+字符串hash)
- zoj 3535 Gao the String II(AC自动机+DP)
- Zoj 3535 Gao the String II (AC自动机+dp)
- ZOJ 3647 Gao the Grid(居然是暴力)
- zoj 3535 Gao the String II (ac自动机+dp)
- 写给新入IT的新人们:多看、多问、多写
- 函数指针的应用(二)
- dos 基本命令
- hdu1010 深搜
- Android:Smali语法中文介绍
- zoj 3672 Gao The Sequence
- Salt stack first sls 文件
- eval(function(p,a,c,k,e,d)系列解密javascript程序
- Java中二进制字节与十六进制互转
- IE10+VS2010不能调用ie调试的解决方案(未能将脚本调试器附加到计算机)
- VC中 snprintf()应该为: _snprintf()
- Kiln:直接运行在iOS设备上的应用开发工具 (github.com)
- 真心不要脸啊
- hive 0.7使用记录