D. Extreme Subtraction(思维+差分构造)

16 篇文章 0 订阅

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;
}

 

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值