题解
2025-09-13 20:29:41
发布于:广东
#include<bits/stdc++.h>
using namespace std;
long long t,n,a[500005],b[500005],c[500005],d[500005],cnt,num;
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
scanf("%ld",&t);
while(t--){
scanf("%ld",&n);
if(n>40000){
cnt=num=0;
for(int i=1;i<=n;i++){
scanf("%ld",&a[i]);
if(a[i]>0){
if(num>0){
cnt=1;
}else{
num=0;
}
}
num+=a[i];
}
}else{
cnt=0;
for(int i=1;i<=n;i++){
a[i]=d[i]=b[i]=c[i]=0;
scanf("%ld",&a[i]);
d[i]=d[i-1]+a[i];
}
stack<int>s,t;
for(int i=n;i>=1;i--){
while(s.size() and a[s.top()]<a[i]){
b[s.top()]=i;
s.pop();
}
s.push(i);
while(t.size() and a[t.top()]<a[n-i+1]){
c[t.top()]=n-i+1;
t.pop();
}
t.push(n-i+1);
}
for(int i=1;i<=n;i++){
if(!c[i]){
c[i]=n+1;
}
}
for(int i=1;i<=n;i++){
for(int j=b[i]+1;j<i;j++){
if(d[i-1]-d[j-1]>0){
cnt=1;
}
}
for(int j=i+1;j<c[i];j++){
if(d[j]-d[i]>0){
cnt=1;
}
}
if(cnt){
break;
}
}
}
if(cnt){
printf("NO\n");
}else{
printf("YES\n");
}
}
return 0;
}
这里空空如也
有帮助,赞一个