• codeforces——contest 864 problemE


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

    ————————————————————————————

    这道题其实就是有人家着火了 一个物品有三个属性 营救所需要的时间ti 必须在di前被救(注意是前) 价值为 pi

    要求输出最多能救的最大价值 并输出方案 即营救了多少物品 并输出这些物品 (按营救顺序

    这样之后呢 我们发现最大价值其实可以背包 而背包的方案输出可以用一个数组记一下转移顺序就可以了

    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    using std::max;
    using std::sort;
    const int M=107;
    int read(){
        int ans=0,f=1,c=getchar();
        while(c<'0'||c>'9'){if(c=='-') f=-1; c=getchar();}
        while(c>='0'&&c<='9'){ans=ans*10+(c-'0'); c=getchar();}
        return ans*f;
    }
    int n,f[M][2007],map[M][2007];
    struct pos{int t,d,p,id;}q[M];
    bool cmp(pos a,pos b){return a.d<b.d;}
    int qu[M],cnt;
    int mx=0,id;
    int main(){
        n=read();
        for(int i=1;i<=n;i++) q[i].t=read(),q[i].d=read()-1,q[i].p=read(),q[i].id=i;
        std::sort(q+1,q+1+n,cmp);
        for(int i=1;i<=n;i++){
            for(int j=q[i].d;j>=q[i].t;j--){
                if(f[i-1][j-q[i].t]+q[i].p>f[i-1][j]){
                    f[i][j]=f[i-1][j-q[i].t]+q[i].p;
                    map[i][j]=j-q[i].t;
                }
                else{f[i][j]=f[i-1][j]; map[i][j]=j;}
            }
            for(int j=q[i].t-1;j;j--) f[i][j]=f[i-1][j],map[i][j]=j;
        }
        for(int j=1;j<=q[n].d;j++) if(f[n][j]>mx) mx=f[n][j],id=j;
        printf("%d
    ",mx);
        int l=n,r=id;
        for (int i=n;i>=1;i--){
            if (map[i][r]<r) qu[++cnt]=q[i].id;
            r=map[i][r];
        }
        printf("%d
    ",cnt);
        for(int i=cnt;i;i--) printf("%d ",qu[i]);
        return 0;
    }
    View Code
  • 相关阅读:
    Controller之daemonset
    Ubuntu下Zmap的安装
    VSCode无法加载PlatformIO按钮可能的原因(踩坑笔记)
    由于更换域名或者IP变更导致WordPressg无法进入后台的问题解决办法
    使用VSCode进行Arduino与ESP32开发配置指南
    Win7下阿米洛机械键盘蓝牙配置
    IIC通讯协议与SPI通讯协议小结
    如何在树莓派上搭建个人博客系统(踩坑笔记)
    STorM32 BGC三轴云台控制板电机驱动电路设计(驱动芯片DRV8313)
    #数据结构#什么是栈及栈的作用
  • 原文地址:https://www.cnblogs.com/lyzuikeai/p/7594813.html
Copyright © 2020-2023  润新知