Codeforce 1469C. Building a Fence(思维)

题目链接

题意:

n n n 个宽度为 1 高度为 k k k 的木板构建一段栅栏 第 i i i 个木板下面的地面高度等于 h i h_i hi 第一个和最后一个木板必须在地面上 其他的木板 底端 可能位于地面或者不高于地面 k − 1 k−1 k1的高度上 相邻的两个木板必须有公共边 也即有重合的部分 问有没有可能建造一个符合所有规则的围栏
在这里插入图片描述

思路:

找到一个点的最大上界,和最小下界,如果最小下界大于最小上界那肯定就不可以.
注意最后要判断一下最后一个是否可以接地.

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

猜你喜欢

转载自blog.csdn.net/weixin_45911397/article/details/114949785