首页 > 其他分享 >CodeForces-1469C Building a Fence

CodeForces-1469C Building a Fence

时间:2022-08-18 10:57:05浏览次数:72  
标签:Building Fence int CodeForces cin 区间 include

Building a Fence

dp 模拟?

维护好可摆放的区间即可,我用的区间是指当前位置可摆放的东西的下边界

区间下限:\(l_i = max(l_{i+1} - k, h_i)\),表示尽量往下放,以及在地面之上

区间上限:\(r_i = min(r_{i-1} - 1, h_i + k - 1)\),表示尽量往上放,且下边界不超过地面的 \(k - 1\)

#include <iostream>
#include <cstdio>
#include <vector>
using namespace std;

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t;
    cin >> t;
    while(t--)
    {
        int n, k;
        cin >> n >> k;
        vector<int>h(n);
        for(int i=0; i<n; i++)
            cin >> h[i];
        int f = 1, l = h[0], r = h[0] + 1;
        for(int i=0; i<n && f; i++)
        {
            l = max(l + 1 - k, h[i]);
            r = min(r - 1, h[i] + k - 1);
            if(r < l || l - h[i] >= k) f = 0;
            r = r + k;
        }
        if(f && l == h[n-1]) cout << "YES\n";
        else cout << "NO\n";
        cout << endl;
    }
    return 0;
}

标签:Building,Fence,int,CodeForces,cin,区间,include
From: https://www.cnblogs.com/dgsvygd/p/16597929.html

相关文章

  • Codeforces Round 81 Same GCDs
    SameGCDs原文题意:给定a,m求x在\([0,m)\)中有多少数字满足\(gcd(a,m)=gcd(a+x,m)\)思路:我们令\(g=gcd(a,m)\)那么\(a=k_1g\),\(m=k_2g\),且\(gcd(k_1,k_2......
  • Codeforces 1713C - Build Permutation
    题意为给出一个长度为n的空数组,数组下标为0至n-1。我们需要在数组中的每个位置上填上合适的数A[i],使得i+A[i]为完全平方数。并且数组最后需为0至n-1的一个排列。......
  • Codeforces Round #814 (Div. 2)
    A.ChipGame题目描述BurenkaandTonyaareplayinganoldBuryatgamewithachiponaboardofn\timesmcells.Atthebeginningofthegame,thechipisl......
  • Codeforces1698F Equal Reversal【构造】
    分析:注意到你无论如何都无法改变a[1]的值,而你要改变a[2]的值时,你就必须要选择一个和a[1]相同的值,然后翻转这一段区间。又可以发现,任意两个数的相邻情况是不会改变的。比......
  • Codeforces Round #814 (Div. 2)
    比赛链接CodeforcesRound#814(Div.2)D2.BurenkaandTraditions(hardversion)给出\(n\)个数,每次可以选择一个区间和一个数,将该区间的所有数异或上该数,代价为区......
  • [Codeforces_gym_102136] I.Permutations again
    传送门DescriptionGivenasequence\(A_i\)consistingof\(N\)integers.Findthenumberofpairs\((L,R)\)forwhichthesubsegment\({A_L,A_{L + 1},......
  • *Codeforces Round #766 (Div. 2) C. Not Assigning(dfs)
    https://codeforces.com/contest/1627/problem/C给你一个n个顶点的树,顶点从1到n,边从1到n-1。树是没有圈的连通无向图。你必须给树的每条边分配整数权重,这样得到的图就是一......
  • CodeForces-1472F New Year's Puzzle
    NewYear'sPuzzle模拟如果尝试从左到右放,就会发现其实放的基本是唯一的,因此考虑直接模拟就好了针对当前列,分成三种状态:状态\(0\):上下都不能放状态\(1\):下面不......
  • Codeforces 阶段性总结提升
    卡在蓝名有一段时间了,对七月份以来的几场cf做一个总结,以求提升。总结提升:(最重要的点)常卡住的在自己平均实力水平以内的题:贪心。https://codeforces.com/contest/170......
  • Codeforces1699E Three Days Grace【数学】【DP】
    分析:一开始觉得是二分答案,发现行不通之后改为枚举最小值。现在我将这若干个数分解,假设分解完之后得到的最小值为$i$,那么我就是要在最小值为$i$的基础上尽量最小化分解的......