[洛谷 P1280]尼克的任务

本文介绍了一种使用线性动态规划方法解决“尼克的任务”问题的算法。通过定义状态f[i]为从1到i可以休息的时间,利用状态转移方程进行求解。文章详细解释了状态转移过程,并提供了完整的AC代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题面

线性DP题,依靠大佬才调出来的我已经不敢说这是简单题了。。

思路:

设f[i]表示从1-i可以休息的时间

状态转移方程;

    if(is[i]==0)
        dp[i]=dp[i+1]+1

    else

        dp[i]=max{dp[i],dp[i+a[cnt].last-1]}

cnt表示当前的任务序号

AC代码:

/* P1280 尼克的任务 */
#include <bits/stdc++.h>
#define int long long
using namespace std;

const int maxn = 1e4 + 10;

struct Node
{
    int st, la;
} a[maxn];

int tot = 1;
int all, n;
int dp[maxn];

bool cmp(const Node &a, const Node &b)
{
    return a.st < b.st;
}

inline int read()
{
    int x = 0, f = 1;
    char ch = getchar();
    while (!isdigit(ch))
        f = (ch == '-') ? -1 : 1, ch = getchar();
    while (isdigit(ch))
        x = x * 10 + (ch - '0'), ch = getchar();
    return x * f;
}
vector<int> b[maxn];
signed main()
{
    all = read(), n = read();
    for (int i = 1; i <= n; i++)
        a[i].st = read(), a[i].la = read();
    sort(a + 1, a + 1 + n, cmp);
    for (int i = 1; i <= n; i++)
        b[a[i].st].push_back(a[i].st + a[i].la - 1);
    for (int i = all; i; i--)
    {
        if (b[i].empty())
            dp[i] = dp[i + 1] + 1;
        else
            for (auto j : b[i])
                dp[i] = max(dp[i], dp[j + 1]);
    }
    cout << dp[1] << '\n';
    return 0;
}

转载于:https://www.cnblogs.com/wyctstf/p/11548798.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值