1285B Just Eat It!

链接:http://codeforces.com/contest/1285/problem/B

B. Just Eat It!

time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Today, Yasser and Adel are at the shop buying cupcakes. There are n cupcake types, arranged from 1 to n on the shelf, and there are infinitely many of each type. The tastiness of a cupcake of type i is an integer ai. There are both tasty and nasty cupcakes, so the tastiness can be positive, zero or negative.

Yasser, of course, wants to try them all, so he will buy exactly one cupcake of each type.

On the other hand, Adel will choose some segment [l,r] (1≤l≤r≤n) that does not include all of cupcakes (he can’t choose [l,r]=[1,n]) and buy exactly one cupcake of each of types l,l+1,…,r.

After that they will compare the total tastiness of the cupcakes each of them have bought. Yasser will be happy if the total tastiness of cupcakes he buys is strictly greater than the total tastiness of cupcakes Adel buys regardless of Adel’s choice.

For example, let the tastinesses of the cupcakes be [7,4,−1]. Yasser will buy all of them, the total tastiness will be 7+4−1=10. Adel can choose segments [7],[4],[−1],[7,4] or [4,−1], their total tastinesses are 7,4,−1,11 and 3, respectively. Adel can choose segment with tastiness 11, and as 10 is not strictly greater than 11, Yasser won’t be happy 😦

Find out if Yasser will be happy after visiting the shop.

Input
Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤104). The description of the test cases follows.

The first line of each test case contains n (2≤n≤105).

The second line of each test case contains n integers a1,a2,…,an (−109≤ai≤109), where ai represents the tastiness of the i-th type of cupcake.

It is guaranteed that the sum of n over all test cases doesn’t exceed 105.

Output
For each test case, print “YES”, if the total tastiness of cupcakes Yasser buys will always be strictly greater than the total tastiness of cupcakes Adel buys regardless of Adel’s choice. Otherwise, print “NO”.

Example
input
3
4
1 2 3 4
3
7 4 -1
3
5 -5 5
output
YES
NO
NO
Note
In the first example, the total tastiness of any segment Adel can choose is less than the total tastiness of all cupcakes.

In the second example, Adel will choose the segment [1,2] with total tastiness 11, which is not less than the total tastiness of all cupcakes, which is 10.

In the third example, Adel can choose the segment [3,3] with total tastiness of 5. Note that Yasser’s cupcakes’ total tastiness is also 5, so in that case, the total tastiness of Yasser’s cupcakes isn’t strictly greater than the total tastiness of Adel’s cupcakes.

思路:

就是找1 ~ n里面有么连续的序列的和大于整个序列的和(这个序列不包括1 ~ n);
首先从1~ n-1里面找子序列和的最大值,然后再从2~ n里面找子序列和的最大值;
对于序列中的某个数 a[ i ],如果当前sum加上 a[ i ]之后变大了,说明这个数可取,如果变小了,就让sum从a[i]开始计算,并时刻更新maxx的值(maxx代表整个过程中sum的最大值);

AC代码:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll a[100005];
int main()
{
	int t;
	cin >>t;
	while(t--)
	{
		int n,cnt=0;
		cin >>n;
		ll sum=0,maxx=-0x3f3f3f3f,res=0;
		for(int i=0;i<n;i++) cin >>a[i],res+=a[i];
		for(int i=0;i<n-1;i++)
		{
			sum+=a[i];
			if(a[i]>sum) sum=a[i];
			maxx=max(maxx,sum);
		}
		sum=0;
		for(int i=n-1;i>0;i--)
		{
			sum+=a[i];
			if(a[i]>sum) sum=a[i];
			maxx=max(maxx,sum);
		}
		if(res>maxx) cout <<"YES"<<endl;
		else cout <<"NO"<<endl;
	}
}

当然也可以用dp来做:思想和上面是一样的,每次取a[i]之前,先考虑加上之前是否大于零,如果大于零就加上这个数,如果小于零,就从当前这个a[i]进行dp

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll dp[100005],a[100005];
int main()
{
	int t;
	cin >>t;
	while(t--)
	{
		int n;
		cin >>n;
		ll sum=0,res=-0x3f3f3f3f;
		for(int i=1;i<=n;i++) cin >>a[i],sum+=a[i];
		for(int i=1;i<n;i++)
		{
			dp[i]=dp[i-1]>=0? dp[i-1]+a[i] : a[i];
			res=max(res,dp[i]);
		}
		memset(dp,0,sizeof dp);
		for(int i=2;i<=n;i++)
		{
			dp[i]=dp[i-1]>=0? dp[i-1]+a[i] : a[i];
			res=max(res,dp[i]);
		}
		if(sum>res) cout <<"YES"<<endl;
		else cout <<"NO"<<endl;
	}
	
	
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值