HDU多校第八场——Fluctuation Limit

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6860

题意:给你n个区间,还有一个k,问你能不能在每个区间内都选择一个数,并且这n个数相邻之间差值不能超过k。
思路:先判断最后能不能得到可行的区间,不行直接输出NO,然后计算每个每个数的取值范围,最后从最后一个可行的区间取一个值,回溯计算到第一个,输出。
(告诫自己一句,做题切勿急躁,容易手忙脚乱,脑子混乱,恶行循环,再水的题也能做成举世难题)
AC:

/*
 * @沉着,冷静!: 噗,这你都信!
 * @LastEditors: HANGNAG
 * @LastEditTime: 2020-08-14 10:10:22
 * @FilePath: \ACM_vscode\杭电多校\main.cpp
 */
#include<algorithm>
#include<string.h>
#include<iostream>
#include<stdio.h>
#include<string>
#include<math.h>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<set>
#define emplace_back push_back
#define pb push_back
using namespace std;
typedef long long LL;
const LL mod = 1e9 + 7;
const double eps = 1e-6;
const LL inf = 0x3f3f3f3f;
const LL N = 2e5 + 10;
LL ans[N];
LL a[N][2];
LL dp[N][2];
int main()
{
    LL t;
    scanf("%lld", &t);
    while(t--)
    {
        LL n, k;
        LL l, r;
        LL ll, rr;
        LL flog = 1;
        scanf("%lld%lld", &n, &k);
        scanf("%lld%lld", &a[1][0], &a[1][1]);
        dp[1][0] = a[1][0];
        dp[1][1] = a[1][1];
        for (LL i = 2; i <= n;i++)
        {
            scanf("%lld%lld", &a[i][0], &a[i][1]);
            dp[i][0] = max(a[i][0], dp[i-1][0]-k);
            dp[i][1] = min(dp[i-1][1]+k, a[i][1]);
            if(dp[i][0]>dp[i][1])
            {
                flog = 0;
            }
           
        }
        ans[n] = dp[n][0];
        for (int i = n - 1; i >= 1;i--)
        {
            ans[i] = max((ans[i + 1] - k), dp[i][0]);
        }
            if (!flog)
            {
                printf("NO\n");
            }
            else
            {
                printf("YES\n%lld", ans[1]);
                for (LL i = 2; i <= n; i++)
                {
                    printf(" %lld", ans[i]);
                }
                printf("\n");
            }
    }
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值