CodeForces - Problem 1442 A - Extreme Subtraction - DP

CodeForces - Problem 1442 A - Extreme Subtraction - DP

这是一道构造题,之前没练过构造.思路是贪心,DP

image-20201117153514326

#include <bits/stdc++.h>
using namespace std;
int n,t;
int v[30000+4];
int a[30000+4],b[30000+4];
int main(){
    scanf("%d",&t);
    while(t--){
        int flag = 1;
        scanf("%d",&n);
        for(int i=1;i<=n;i++){
            scanf("%d",&v[i]);
        }
        a[1]=v[1],b[1]=0;
        for(int i=2;i<=n;i++){
            a[i]=min(a[i-1],v[i]-b[i-1]);
            b[i]=v[i]-a[i];
            if(b[i]<0||b[i]>v[i]){
                flag=0;
                break;
            }
        }

        if(flag){
            puts("YES");
        }else{
            puts("NO");
        }
    }

    system("pause");
    return 0;
}
posted @ 2020-11-17 15:35  popozyl  阅读(135)  评论(0编辑  收藏  举报