864E(dp + 线段树优化)

E. Fire
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Polycarp is in really serious trouble — his house is on fire! It's time to save the most valuable items. Polycarp estimated that it would take ti seconds to save i-th item. In addition, for each item, he estimated the value of di — the moment after which the item i will be completely burned and will no longer be valuable for him at all. In particular, if ti ≥ di, then i-th item cannot be saved.

Given the values pi for each of the items, find a set of items that Polycarp can save such that the total value of this items is maximum possible. Polycarp saves the items one after another. For example, if he takes item a first, and then item b, then the item a will be saved in ta seconds, and the item b — in ta + tb seconds after fire started.

Input

The first line contains a single integer n (1 ≤ n ≤ 100) — the number of items in Polycarp's house.

Each of the following n lines contains three integers ti, di, pi (1 ≤ ti ≤ 201 ≤ di ≤ 2 0001 ≤ pi ≤ 20) — the time needed to save the item i, the time after which the item i will burn completely and the value of item i.

Output

In the first line print the maximum possible total value of the set of saved items. In the second line print one integer m — the number of items in the desired set. In the third line print m distinct integers — numbers of the saved items in the order Polycarp saves them. Items are 1-indexed in the same order in which they appear in the input. If there are several answers, print any of them.

Examples
input
3
3 7 4
2 6 5
3 7 6
output
11
2
2 3 
input
2
5 6 1
3 3 5
output
1
1
1 
Note

In the first example Polycarp will have time to save any two items, but in order to maximize the total value of the saved items, he must save the second and the third item. For example, he can firstly save the third item in 3 seconds, and then save the second item in another 2 seconds. Thus, the total value of the saved items will be 6 + 5 = 11.

In the second example Polycarp can save only the first item, since even if he immediately starts saving the second item, he can save it in 3 seconds, but this item will already be completely burned by this time.

解题思路:直接dp, 我们先按每个物品的截止日期从小到大排个序,令dp[i][j]表示前i个物品,且第i个物品一定选,当前是第j天能获得的最大值,然后直接从后面所有与状态i, j不冲突的状态转移就行,中间过程记录最优解的前驱就行,可能是我的状态定义的有点不好,这样做的最坏复杂度会达到O(100 * 2000 * 100 * 2000),显然不能接受,我们注意到,我们不用每次从后面所有的状态转移,我们用一棵线段树维护最优解就行。

#include <bits/stdc++.h>
using namespace std;
int n;
int inf = 0x3f3f3f3f;
int dp[110][2010];
int pre1[110][2010];
int pre2[110][2010];
struct node{
    int id;
    int t;
    int d;
    int p;
}Node[110];
struct tree{
    int l, r;
    int pr1, pr2;
    int Max;
    tree(){
        pr1 = pr2 = 0;
        Max = -inf;
    }
}Tree[2010<<2];
struct info{
     int i, j;
     int value;
}Info[2010];
int num;
void pushUp(int i)
{
    int lson = i<<1;
    int rson = lson|1;
    if(Tree[lson].Max >= Tree[rson].Max)
    {
        Tree[i].Max = Tree[lson].Max;
        Tree[i].pr1 = Tree[lson].pr1;
        Tree[i].pr2 = Tree[lson].pr2;
    }
    else
    {
        Tree[i].Max = Tree[rson].Max;
        Tree[i].pr1 = Tree[rson].pr1;
        Tree[i].pr2 = Tree[rson].pr2;
    }
}
void build(int i, int l, int r)
{
    Tree[i].l = l;
    Tree[i].r = r;
    if(l == r)
    {
        if(l == 0) Tree[i].Max = 0;
        else Tree[i].Max = -inf;
        return;
    }
    int mid = (l + r)>>1;
    int f = i;
    i <<= 1;
    build(i, l, mid);
    build(i|1, mid + 1, r);
    pushUp(f);
}
void update(int i, int loc, int mm, int status1, int status2)
{
    if(Tree[i].l == Tree[i].r)
    {
        if(mm > Tree[i].Max)
        {
            Tree[i].Max = mm;
            Tree[i].pr1 = status1;
            Tree[i].pr2 = status2;
        }
        return;
    }
    int f = i;
    i <<= 1;
    if(loc <= Tree[i].r) update(i, loc, mm, status1, status2);
    else update(i|1, loc, mm, status1, status2);
    pushUp(f);
}
int ss1, ss2;
int query(int i, int l, int r)
{
    if(Tree[i].l == l && Tree[i].r == r)
    {
        ss1 = Tree[i].pr1;
        ss2 = Tree[i].pr2;
        return Tree[i].Max;
    }
    i <<= 1;
    if(r <= Tree[i].r) return query(i, l, r);
    else if(l >= Tree[i|1].l) return query(i|1, l, r);
    else
    {
        int judge1 = query(i, l, Tree[i].r);
        int judge2 = query(i|1, Tree[i|1].l, r);
        if(judge2 >= judge1) return judge2;
        return query(i, l, Tree[i].r);
    }
}
int MM;
void init()
{
    memset(dp, -inf, sizeof(dp));
    dp[0][0] = 0;
    build(1, 0, MM);
    for(int i = 1; i <= n; i++)
    {
        num = 0;
        for(int j = 0; j <= MM; j++)
        {
            pre1[i][j] = 0;
            pre2[i][j] = 0;
            if(j >= Node[i].d) continue;
            if(Node[i].t >= Node[i].d) continue;
            if(j - Node[i].t < 0) continue;
            ss1 = ss2 = 0;
            int M = query(1, 0, j - Node[i].t);
            dp[i][j] = M + Node[i].p;
            pre1[i][j] = ss1;
            pre2[i][j] = ss2;
            Info[++num].i = i;
            Info[num].j = j;
            Info[num].value = dp[i][j];
        }
        for(int j = 1; j <= num; j++) update(1, Info[j].j, Info[j].value, Info[j].i, Info[j].j);
    }
}
bool cmp(node n1, node n2)
{
    return n1.d < n2.d;
}
int ans[110];
int res;
int main()
{
    while(~scanf("%d", &n))
    {
        MM = 0;
        for(int i = 1; i <= n; i++)
        {
            scanf("%d%d%d", &Node[i].t, &Node[i].d, &Node[i].p);
            Node[i].id = i;
            MM = max(MM, Node[i].d);
        }
        sort(Node + 1, Node + n + 1, cmp);
        init();
        int Max = 0;
        int sta1 = 0;
        int sta2 = 0;
        int m = 0;
        for(int i = 1; i <= n; i++)
        {
            for(int j = 0; j <= MM; j++)
            {
                if(dp[i][j] > Max)
                {
                   Max = dp[i][j];
                   sta1 = i;
                   sta2 = j;
                }
            }
        }
        if(Max > 0)
        {
            res = 0;
            while(sta1 && sta2)
            {
                ans[++res] = Node[sta1].id;
                int xx = pre1[sta1][sta2];
                int yy = pre2[sta1][sta2];
                sta1 = xx;
                sta2 = yy;
            }
            printf("%d\n", Max);
            printf("%d\n", res);
            int sum = 0;
            for(int i = res; i >= 1; i--)
            {
                if(i == 1) printf("%d\n", ans[i]);
                else printf("%d ", ans[i]);
            }
        }
        else printf("0\n0\n");

    }
    return 0;
}


如果你希望**不使用线段树**,而使用**暴力 DP**来解决这个问题,那么我们可以采用一个**基于区间的动态规划方法**。 --- ### 🧠 思路回顾 - 目标是覆盖时间段 `[M, E]`。 - 每头奶牛提供一个工作区间 `[T1, T2]` 和一个报酬 `S`。 - 我们要选择若干奶牛,使得它们的工作区间**完全覆盖 [M, E]**,并且总报酬最小。 - 如果无法覆盖,输出 `-1`。 --- ### ✅ 暴力 DP 解法 #### 1. 动态规划定义 定义 `dp[i]` 表示覆盖到时间点 `i` 所需的最小费用。 初始状态: - `dp[M - 1] = 0`(表示在开始时间前不需要费用) - `dp[i] = INF` 表示时间点 `i` 无法被覆盖 #### 2. 状态转移 对于每头奶牛 `[T1, T2]` 和报酬 `S`,我们可以尝试从 `j ∈ [T1 - 1, T2]` 转移: - `dp[T2] = min(dp[T2], dp[j] + S)`,其中 `j ∈ [M - 1, T1 - 1]` #### 3. 最终答案 - 如果 `dp[E] == INF`,说明无法覆盖,输出 `-1` - 否则输出 `dp[E]` --- ### ✅ C++ 暴力 DP 实现 ```cpp #include <bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; const int MAX_TIME = 86400 + 10; // 最大时间点 +1 int N, M, E; int dp[MAX_TIME]; // dp[i] 表示覆盖到时间 i 的最小花费 struct Cow { int t1, t2, s; }; int main() { cin >> N >> M >> E; vector<Cow> cows(N); for (int i = 0; i < N; ++i) { int t1, t2, s; cin >> t1 >> t2 >> s; cows[i] = {t1, t2, s}; } // 初始化 dp 数组 fill(dp, dp + MAX_TIME, INF); dp[M - 1] = 0; // 暴力 DP for (int i = 0; i < N; ++i) { int t1 = cows[i].t1; int t2 = cows[i].t2; int s = cows[i].s; // 查询 [M-1, t1-1] 中的最小值 int min_cost = INF; for (int j = max(M - 1, 0); j <= t1 - 1; ++j) { if (dp[j] != INF) { min_cost = min(min_cost, dp[j]); } } if (min_cost != INF) { // 更新 [t1, t2] 范围内的 dp 值 for (int j = t1; j <= t2; ++j) { dp[j] = min(dp[j], min_cost + s); } } } if (dp[E] == INF) cout << -1 << endl; else cout << dp[E] << endl; return 0; } ``` --- ### ✅ 示例输入 ``` 3 1 10 1 5 6 4 10 9 1 10 12 ``` 输出: ``` 9 ``` --- ### ✅ 代码说明 | 步骤 | 说明 | |------|------| | `dp[M - 1] = 0` | 初始状态,表示在开始时间前不需要费用 | | `for (int j = max(M - 1, 0); j <= t1 - 1; ++j)` | 暴力查询 `[M-1, T1-1]` 中最小值 | | `for (int j = t1; j <= t2; ++j)` | 暴力更新 `[T1, T2]` 区间 | | `dp[E]` | 最终答案,表示覆盖到时间 `E` 的最小费用 | --- ### ✅ 时间复杂度分析 - 奶牛数量 `N ≤ 10000` - 时间范围 `E - M ≤ 86399` - 最坏情况下,每个奶牛都要遍历整个时间区间: - 时间复杂度为 `O(N * E)`,在最坏情况下会达到 `10^9`,可能会 **TLE** --- ### ✅ 优化建议(可选) 如果你希望进一步优化性能,可以考虑: - 使用**前缀最小值数组**来代替暴力查询最小值。 - 使用**区间 DP** 或 **线段树优化转移**,将时间复杂度降低到 `O(N log E)` --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值