https://codeforces.com/contest/1443/problem/D
题意:使序列的前缀-1或者后缀-1,问能否构造成全0.
思路:对序列的区间操作,考虑到转化到两点上去,而且全0意味着差分数组是0且第一个数是0.由此尝试差分。
d[1]=a[1].
对操作一而言,差分使得d[1]-1,d[i+1]+1;也就是使得序列中的差分数是负数的变成正数。
对于操作二而言,差分使得d[i]-1,d[n+1]+1;也就是使得序列中的差分数是正数的变成负数。
那么要想差分数组全是0,首先要让除d[1]外的数组都是0,因为我们可以无限次对d[1]操作。那么也就是能够满足让d[2~n]中都为0,负数加到0,正数减到0.
即考虑第一个数大小能不能>=后面差分数的负数的绝对值的和。
#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<cstdio>
#include<algorithm>
#define debug(a) cout<<#a<<"="<<a<<endl;
using namespace std;
const int maxn=3e4+100;
typedef long long LL;
LL a[maxn],cnt[maxn];
int main(void)
{
cin.tie(0);std::ios::sync_with_stdio(false);
LL t;cin>>t;
while(t--)
{
LL n;cin>>n;
for(LL i=1;i<=n;i++) cin>>a[i];
for(LL i=1;i<=n;i++) cnt[i]=a[i]-a[i-1];
LL sum=0;
for(LL i=1;i<=n;i++){
if(cnt[i]<0) sum+=abs(cnt[i]);
}
if(sum<=a[1]){
cout<<"YES"<<endl;
}
else cout<<"NO"<<endl;
}
return 0;
}