Codeforces Round #613 (Div. 2)B. Just Eat It!

B. Just Eat It!

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Today, Yasser and Adel are at the shop buying cupcakes. There are nn cupcake types, arranged from 11 to nn on the shelf, and there are infinitely many of each type. The tastiness of a cupcake of type ii is an integer aiai. 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][l,r] (1≤l≤r≤n)(1≤l≤r≤n) that does not include all of cupcakes (he can't choose [l,r]=[1,n][l,r]=[1,n]) and buy exactly one cupcake of each of types l,l+1,…,rl,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][7,4,−1]. Yasser will buy all of them, the total tastiness will be 7+4−1=107+4−1=10. Adel can choose segments [7],[4],[−1],[7,4][7],[4],[−1],[7,4] or [4,−1][4,−1], their total tastinesses are 7,4,−1,117,4,−1,11 and 33, respectively. Adel can choose segment with tastiness 1111, and as 1010 is not strictly greater than 1111, 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 tt (1≤t≤1041≤t≤104). The description of the test cases follows.

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

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

It is guaranteed that the sum of nn over all test cases doesn't exceed 105105.

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".

题意:给定一段序列,总和设为A,要是最大连续子序列B>=A则输出NO,否则输出YES,要注意这里的子序列是真子序列

回忆一下最大连续子序列是怎么做的那呢,最简单的dp嘛,ok此题结束

#include<bits/stdc++.h>
#define ll long long
#define rg register ll
#define inf 2147483647
using namespace std;
ll n,a[100005];

int main()
{
    cin>>n;
    for(rg i=1;i<=n;i++)
    {
        ll k,sum=0,sum1=0,cnt=0,maxx=-inf,maxcnt=0;
        cin>>k;
        for(rg i=1;i<=k;i++)
        {
            cin>>a[i];
            sum+=a[i],sum1+=a[i],cnt++;
            if(sum1<=0)sum1=0,cnt=0;
            if(maxx<sum1)
            {
                maxcnt=cnt;
                maxx=sum1;
            }
        }
        if(sum==maxx&&maxcnt==k)cout<<"YES"<<endl;
        else cout<<"NO"<<endl;
    }
    while(1)getchar();
    return 0;
}
发布了211 篇原创文章 · 获赞 137 · 访问量 20万+

猜你喜欢

转载自blog.csdn.net/weixin_43798170/article/details/104044298