codeforce E. Fire背包

本文介绍了一种解决特定背包问题的算法,该问题涉及在房屋火灾中选择最有价值且能在限定时间内抢救出来的物品。通过将问题转化为一维背包问题,并采用排序优化,实现了高效的求解。

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

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 ≤ 20, 1 ≤ di ≤ 2 000, 1 ≤ 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.

 

典型背包题

但是要排序先,因为如果有一个任务的deadline是8,时间是5,,然后价值是100861,另一个任务的deadline是2,时间是1,价值是1,那么第二个任务不会被计入,就产生了错误

 

就是不具有那种一般背包的元素随便放的条件这个是有一个限制条件在deadline前,那么从小到大用deadline排序就可以变成基本的一维背包了

#include<cstdio>
#include<vector>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=1058;
struct node
{
    int ed,ne,val,id;
    bool operator < (const node &A)const{
      return ed<A.ed;
    }
} e[N];
struct ct
{
    int val;
    vector<int>s;
    ct()
    {
        val=0;
        s.clear();
    }
} dp[N<<1];
int main()
{
    int maxx=0,n;
    scanf("%d",&n);
    for(int i=1; i<=n; ++i)
    {
        scanf("%d%d%d",&e[i].ne,&e[i].ed,&e[i].val);
        maxx=max(maxx,e[i].ed);
        e[i].id=i;
    }
    sort(e+1,e+n+1);
    for(int i=1; i<=n; ++i)
    {
        if(e[i].ed<=e[i].ne) continue;
        for(int j=e[i].ed; j>=1; --j)
        {
            if(j-e[i].ne<=0) break;
            if(dp[j].val<dp[j-e[i].ne].val+e[i].val)
            {
                dp[j].val=dp[j-e[i].ne].val+e[i].val;
                dp[j].s.clear();
                for(int k=0; k<(int)dp[j-e[i].ne].s.size(); ++k) dp[j].s.push_back(dp[j-e[i].ne].s[k]);
                dp[j].s.push_back(e[i].id);
            }
        }
    }
    int k=maxx;
    for(int i=1;i<=maxx;++i) if(dp[i].val>dp[k].val) k=i;
    printf("%d\n%d\n",dp[k].val,dp[k].s.size());
    for(int i=0; i<(int)dp[k].s.size(); ++i) printf("%d ",dp[k].s[i]);
    puts("");
}

这个是别人的干净清爽的代码。。

 

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N = 101;
const int M = 2001;
int n;
int dp[M], vis[N][M];
struct node {
    int t, d, p, id;
    bool operator < (const node&r) const{
        return d < r.d;
    }
}a[N];
int b[N];
int main() {
    int i, j, k, t, ed = 0, cnt = 0;
    memset(dp, 0, sizeof(dp)); memset(vis, 0, sizeof(vis));
    scanf("%d", &n);
    for(i = 1 ; i <= n; ++i) {
        scanf("%d%d%d", &a[i].t, &a[i].d, &a[i].p);
        a[i].id = i;
    }
    sort(a+1, a+1+n);
    for(i = 1; i <= n; ++i) {
        t = a[i].t;
        for(j = a[i].d-1; j >= t; --j) {
            if(dp[j] < dp[j-t]+a[i].p) {
                dp[j] = dp[j-t] + a[i].p;
                vis[i][j] = 1;
            }
        }
    }
    for(i = 1; i < a[n].d; ++i) if(dp[i]>dp[ed]) ed = i;
    printf("%d\n", dp[ed]);
    for(i = n; i >= 1; --i) {
        if(vis[i][ed]) {b[cnt++] = a[i].id; ed -= a[i].t;}
    }
    printf("%d\n", cnt);
    for(i = cnt-1; i > 0; --i) printf("%d ", b[i]);
    if(cnt) printf("%d\n", b[0]);
    return 0;
}

 

转载于:https://www.cnblogs.com/mfys/p/7598202.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值