Codeforces 864E Fire【排序+背包dp】

面对房屋火灾,Polycarp需要制定策略,在有限时间内抢救家中物品。每件物品有其抢救时间和价值,且超过一定时间将无法抢救。任务是找出最大价值的物品集合及抢救顺序,涉及动态规划算法。

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 tiseconds 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

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 2seconds. 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 3seconds, but this item will already be completely burned by this time.


题目大意:

给出N个物品,每个物品拿取需要ti时间,di之间之后这个物品就不能拿了,物品的价值为pi,问怎样拿能够拿最多价值的物品,问这些物品拿取的顺序。
 

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
#include<queue>

using namespace std;

struct node
{
    int t,d,p,id;
}itm[105];

bool cmp(node x,node y)
{
    return x.d<y.d;
}
int n;

int dp[125][2005];
int id[125][2005];
int pre[125][2005];

int cnt;
int anss[125];
int main()
{
    while(~scanf("%d",&n))
    {
        cnt=0;
        for(int j=0;j<=120;j++)
        for(int i=0;i<=2001;i++)
        {
            dp[j][i]=0;
            id[j][i]=0;
            pre[j][i]=0;
        }
        for(int i=1;i<=n;i++)
        {
            scanf("%d%d%d",&itm[i].t,&itm[i].d,&itm[i].p);
            itm[i].id=i;
        }
        sort(itm+1,itm+(1+n),cmp);

        for(int i=1;i<=n;i++)
        {
            for(int j=2000;j>=0;j--)
            {
                dp[i][j]=dp[i-1][j];
                id[i][j]=0;
                pre[i][j]=j;

                if(j<itm[i].d && (j-itm[i].t)>=0)
                {
                    if(dp[i][j]<dp[i-1][j-itm[i].t]+itm[i].p)
                    {
                        dp[i][j]=dp[i-1][j-itm[i].t]+itm[i].p;
                        id[i][j]=itm[i].id;
                        pre[i][j]=j-itm[i].t;
                    }
                }
            }
        }
        int ansid=0;
        int ans=0;
        for(int i=0;i<=2000;i++)
            if(ans<dp[n][i])
            {
                ans=dp[n][i];
                ansid=i;
            }
        int tid=n;
        while(ansid!=0)
        {
            if(id[tid][ansid]!=0)
            {
               anss[cnt]=id[tid][ansid];
               cnt++;
            }
            ansid=pre[tid][ansid];
            tid--;
        }
        printf("%d\n",ans);
        printf("%d\n",cnt);
        for(int i=cnt-1;i>=0;i--)
            printf("%d ",anss[i]);
        printf("\n");
    }
}

 

本系统旨在构建一套面向高等院校的综合性教务管理平台,涵盖学生、教师及教务处三个核心角色的业务需求。系统设计着重于实现教学流程的规范化与数据处理的自动化,以提升日常教学管理工作的效率与准确性。 在面向学生的功能模块中,系统提供了课程选修服务,学生可依据培养方案选择相应课程,并生成个人专属的课表。成绩查询功能支持学生查阅个人各科目成绩,同时系统可自动计算并展示该课程的全班最高分、平均分、最低分以及学生在班级内的成绩排名。 教师端功能主要围绕课程与成绩管理展开。教师可发起课程设置申请,提交包括课程编码、课程名称、学分学时、课程概述在内的新课程信息,亦可对已开设课程的信息进行更新或撤销。在课程管理方面,教师具备录入所授课程期末考试成绩的权限,并可导出选修该课程的学生名单。 教务处作为管理中枢,拥有课程审批与教学统筹两大核心职能。课程设置审批模块负责处理教师提交的课程申请,管理员可根据教学计划与资源情况进行审核批复。教学安排模块则负责全局管控,包括管理所有学生的选课最终结果、生成包含学号、姓名、课程及成绩的正式成绩单,并能基于选课与成绩数据,统计各门课程的实际选课人数、最高分、最低分、平均分以及成绩合格的学生数量。 资源来源于网络分享,仅用于学习交流使用,请勿用于商业,如有侵权请联系我删除!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值