zoj 3672 Gao The Sequence

题目链接: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;
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值