Codeforce 1469C. Building a Fence (Thinking)

Topic link

Title:

Use nnn width is 1 and height iskkK ’s planks to construct a fence sectioniiThe height of the ground under the i planks is equal tohi h_ihiThe first and last planks must be on the ground. The bottom ends of other planks may be on the ground or not higher than the ground k − 1 k−1The two adjacent boards at the height of k 1 must have a common edge, that is, have overlapping parts. Is it possible to build a fence that meets all the rules?
Insert picture description here

Ideas:

Find the maximum upper bound and the minimum lower bound of a point. If the minimum lower bound is greater than the minimum upper bound, it must not be possible.
Note that at the end, you must judge whether the last one can be grounded.

#include <set>
#include <map>
#include <queue>
#include <string>
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <math.h>
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
typedef pair<ll, ll> pii;
#define mem(a,x) memset(a,x,sizeof(a))
#define debug(x) cout << #x << ": " << x << endl;
#define rep(i,n) for(int i=0;i<(n);++i)
#define repi(i,a,b) for(int i=int(a);i<=(b);++i)
#define repr(i,b,a) for(int i=int(b);i>=(a);--i)
const int maxn = 5e5 + 1010;
#define inf 0x3f3f3f3f
#define sf scanf
#define pf printf
const int mod = 998244353;
const int MOD = 10007;

inline int read()
{
    
    
    int x = 0;
    bool t = false;
    char ch = getchar();
    while((ch < '0' || ch > '9') && ch != '-')ch = getchar();
    if(ch == '-')t = true, ch = getchar();
    while(ch <= '9' && ch >= '0')x = x * 10 + ch - 48, ch = getchar();
    return t ? -x : x;
}

/*
vector<ll> m1;
vector<ll> m2;
priority_queue<ll , vector<ll> , greater<ll> > mn;//上  小根堆 		小到大
priority_queue<ll , vector<ll> , less<ll> > mx;//下   	大根堆  	大到小
*/
ll n, m;
ll a[maxn], b[maxn];
string str;
ll dp[50][50];

#define read read()
int main()
{
    
    
    ll t;
    cin >> t;

    while(t--)
    {
    
    
        cin >> n >> m;
        for(int i = 1; i <= n; i++)
        {
    
    
            scanf("%lld", &a[i]);
        }
        bool q = 1;
        ll l=a[1],r=a[1];
        for(int i=2;i<=n;i++){
    
    
        	l=max(l-m+1,a[i]);///最低
        	r=min(r+m-1,a[i]+m-1);///最高
        	if(l>r){
    
    ///不符合条件
        		q=0;
        		break;
        	}
        }

        if (a[n] < l || a[n] > r)q= false;
        if(q)
        {
    
    
            puts("YES");
        }
        else puts("NO");

    }
    return 0;
}

Guess you like

Origin blog.csdn.net/weixin_45911397/article/details/114949785