CF1285B Just Eat It! 题解

题目

链接

https://www.luogu.com.cn/problem/CF1285B

字面描述

题面翻译

题意简述

给定长度为 n n n 的数列 a = [ a 1 , a 2 , . . . , a n ] a=[a_1,a_2,...,a_n] a=[a1,a2,...,an]

Yasser 会选择所有的 n n n 个数,并算出它们的和 ∑ a i \sum a_i ai

Adel 会选择两个正整数 l , r ( 1 ≤ l ≤ r ≤ n ) l,r(1\leq l \leq r \leq n) l,r(1lrn),并算出 ∑ i = l r a i \sum_{i=l}^{r} a_i i=lrai。Adel 不能选择 l = 1 , r = n l=1,r=n l=1,r=n

如果 Yasser 算出的和在任意情况下(即 Adel 选取任意的 l , r l,r l,r 都是如此)严格大于 Adel 算出的,那么 Yasser 会开心。否则 Yasser 不会开心。

请你判断 Yasser 是否开心。

输入格式

本题有多组数据

第一行一个正整数 t ( 1 ≤ t ≤ 1 0 4 ) t(1\leq t \leq 10^4) t(1t104),表示数据的组数。

对于每组数据,第一行一个正整数 n ( 2 ≤ n ≤ 1 0 5 ) n(2\leq n \leq 10^5) n(2n105)

接下来一行 a 1 , a 2 , . . . , a n ( − 1 0 9 ≤ a i ≤ 1 0 9 ) a_1,a_2,...,a_n(-10^{9} \leq a_i \leq 10^9) a1,a2,...,an(109ai109)

保证 ∑ n \sum n n 不会超过 1 0 5 10^5 105

输出格式

对于每组数据,如果 Yasser 会开心,输出 YES,否则输出 NO

翻译贡献者 U108949

题目描述

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 $ a_i $ . 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 \le l \le r \le 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, \dots, 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.

输入格式

Each test contains multiple test cases. The first line contains the number of test cases $ t $ ( $ 1 \le t \le 10^4 $ ). The description of the test cases follows.

The first line of each test case contains $ n $ ( $ 2 \le n \le 10^5 $ ).

The second line of each test case contains $ n $ integers $ a_1, a_2, \dots, a_n $ ( $ -10^9 \le a_i \le 10^9 $ ), where $ a_i $ 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 $ 10^5 $ .

输出格式

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

样例 #1

样例输入 #1
3
4
1 2 3 4
3
7 4 -1
3
5 -5 5
样例输出 #1
YES
NO
NO

提示

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 ≤ l ≤ r ≤ n ( l ≠ 1 或 r ≠ n ) 1≤l≤r≤n(l≠1或r≠n) 1lrn(l=1r=n)没有其他限制

导致枚举 l 和 r l和r lr的时间复杂度就有 O ( n 2 ) O(n^2) O(n2)

1 ≤ n ≤ 1 0 5 1≤n≤10^5 1n105
∴TLE无疑

现在该优化了

∵题目只说了Yasser的值严格大于Adel的值
∴直接把Adel 的值拉成最大,这样比较更经济有效。我们只需要再维护一组 m i [ ] 数组 mi[]数组 mi[]数组 m i [ i ] mi[i] mi[i]来记录 f [ 1 ] 到 f [ i − 1 ] f[1]到f[i-1] f[1]f[i1]最小的值,把 f [ i ] − m i [ i ] f[i]-mi[i] f[i]mi[i]的值与Yasser的值比较。

而求 m i [ ] 数组 mi[]数组 mi[]数组的时间复杂度为 O ( n ) O(n) O(n)
需要特判 i = n i=n i=n时的情况

AC!

代码实现

#include<bits/stdc++.h>
#define ll long long
using namespace std;

const int maxn=1e5+10;
const ll inf=1e15+10;
int t,n;
ll tot;
ll a[maxn],f[maxn],mi[maxn];
int main(){
	scanf("%d",&t);
	while(t--){
		scanf("%d",&n);
		tot=0;
		bool flag=true;
		for(int i=1;i<=n;i++){
			scanf("%lld",&a[i]);
			tot+=a[i];//Yasser的值
			f[i]=f[i-1]+a[i];
		}
		mi[1]=0;
		for(int i=2;i<=n;i++)mi[i]=min(mi[i-1],f[i-1]);
		//printf("tot = %lld\n",tot);
		for(int i=1;i<n;i++){
			ll x=f[i]-mi[i];
			//printf("x = %lld\n",x);
			if(x>=tot){
				flag=false;
				break;
			}
		}
		ll cnt=inf;
		for(int i=1;i<n;i++)cnt=min(cnt,f[i]);
		if(f[n]-cnt>=tot)flag=false;
		if(flag)printf("YES\n");
		else printf("NO\n");
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

materialistOier

我只是一名ssfoier

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值