CF864E:E. Fire(01背包 & 路径输出)

本文介绍了一种在火灾场景下,如何最大化救援物品价值的算法。通过动态规划的方法,在考虑物品救援时间和燃烧时限的情况下,确定最优救援顺序。

摘要生成于 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 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 ≤ 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 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件物品可以救出,每一件有三个值a,b,c,表示需要救它要a时间,且在b时间该物品就立刻烧毁,c为它的价值,问最多可以救出多少价值,并输出救的顺序。

思路:明显的背包dp,由于本题有“b”属性的限制,b小的物品不能从b大的状态转移过来,于是需要对b排序,先处理b小的物品。路径输出的话范围小可以用vector直接搞,也可以用数组标记一下。

# include <iostream>
# include <cstdio>
# include <cstring>
# include <vector>
# include <algorithm>
using namespace std;
const int maxn = 2003;
int a[maxn], b[maxn], c[maxn], dp[maxn];
int id[maxn];
vector<int>v[maxn];
bool cmp(int x, int y)
{
    return b[x] < b[y];
}
int main()
{
    int n;
    scanf("%d",&n);
    for(int i=0; i<n; ++i)
        scanf("%d%d%d",&a[i],&b[i],&c[i]),id[i] = i;
    sort(id, id+n, cmp);
    for(int k=0; k<n; ++k)
    {
        int i = id[k];
        if(a[i]>=b[i]) continue;
        for(int j=b[i]-1; j>=a[i]; --j)
        {
            if(dp[j-a[i]]+c[i] > dp[j])
            {
                dp[j] = dp[j-a[i]]+c[i];
                v[j].clear();
                v[j] = v[j-a[i]];
                v[j].emplace_back(i);
            }
        }
    }
    int imax=0, num;
    for(int i=0; i<=2000;++i)
        if(dp[i] > imax)
            imax = dp[i], num=i;
    printf("%d\n",imax);
    printf("%d\n",v[num].size());
    for(auto i : v[num])
        printf("%d ",i+1);
    return 0;
}


# include <iostream>
# include <cstdio>
# include <cstring>
# include <stack>
# include <algorithm>
using namespace std;
const int maxn = 2003;
int a[maxn], b[maxn], c[maxn], dp[maxn];
int id[maxn], cho[maxn], way[103][maxn];
bool cmp(int x, int y)
{
    return b[x] < b[y];
}
int main()
{
    int n, imax, ans=0, num;
    scanf("%d",&n);
    for(int i=1; i<=n; ++i)
        scanf("%d%d%d",&a[i],&b[i],&c[i]),id[i] = i;
    sort(id+1, id+n+1, cmp);
    for(int k=1; k<=n; ++k)
    {
        int i = id[k];
        if(a[i]>=b[i]) continue;
        for(int j=b[i]-1; j>=a[i]; --j)
        {
            if(dp[j-a[i]]+c[i] > dp[j])
            {
                dp[j] = dp[j-a[i]]+c[i];
                cho[j] = i;
                way[i][j] = cho[j-a[i]];
            }
            if(dp[j] > ans)
            {
                ans = dp[j];
                imax = j;
                num = i;
            }
        }
    }
    printf("%d\n",ans);
    if(ans == 0) return 0*puts("0");
    stack<int>st;
    while(imax && num)
    {
        st.push(num);
        int tmp = imax;
        imax -= a[num];
        num = way[num][tmp];
    }
    printf("%d\n",st.size());
    while(!st.empty())
    {
        printf("%d ",st.top());
        st.pop();
    }
    return 0;
}


Line 11624: 01-02 16:07:38.066 1864 3648 D TorchManagerService: onTorchModeChanged, mbTorchMode: true, mbDoSetTorchModeFalseByUs: false Line 11627: 01-02 16:07:38.066 3864 4015 I SystemUi--QuickSettings: FlashlightController--&gt;onTorchModeChanged setTorchMode=true cameraId=0 Line 11733: 01-02 16:07:38.225 8570 8570 F DEBUG : #04 pc 0000000000012770 /vendor/lib64/mt6991/libmtkcam_devicemgr.so (NSCam::CameraDeviceManagerBase::setTorchMode(std::__1::basic_string&lt;char, std::__1::char_traits&lt;char&gt;, std::__1::allocator&lt;char&gt;&gt; const&amp;, unsigned int)+464) (BuildId: d7dac32de39179042a63e9768c3c3cec) Line 11734: 01-02 16:07:38.225 8570 8570 F DEBUG : #05 pc 0000000000010bd0 /vendor/lib64/mt6991/libmtkcam_hal_android_device.so (mcam::android::ACameraDevice::setTorchMode(mcam::android::TorchMode)+96) (BuildId: 06829189d08cf0bd9ebdddfedaf9c685) Line 11735: 01-02 16:07:38.225 8570 8570 F DEBUG : #06 pc 0000000000018080 /vendor/lib64/mt6991/libmtkcam_hal_aidl_device.so (mcam::aidl::AidlCameraDevice::setTorchMode(bool)+304) (BuildId: 0e4806974081ccb6822e9018551e3626) Line 12430: 01-02 16:07:45.215 1411 7059 E cameraserver: setTorchMode Unable to set torch mode: Line 12432: 01-02 16:07:45.215 1411 7059 E CameraService: setTorchMode: Setting torch mode of camera &quot;0&quot; to 0 failed: Broken pipe (-32) Line 12450: 01-02 16:07:45.218 8532 8554 W roid.camera.cts: Long monitor contention with owner Instr: androidx.test.runner.AndroidJUnitRunner (8561) at void android.hardware.camera2.CameraManager$CameraManagerGlobal.setTorchMode(java.lang.String, boolean, android.content.AttributionSourceState, int)(CameraManager.java:2938) waiters=0 in void android.hardware.camera2.CameraManager$CameraManagerGlobal.onTorchStatusChanged(int, java.lang.String, int) for 7.152s Line 12494: 01-02 16:07:45.219 8532 8561 E TestRunner: android.hardware.camera2.CameraAccessException: CAMERA_ERROR (3): setTorchMode:3299: Setting torch mode of camera &quot;0&quot; to 0 failed: Broken pipe (-32) Line 12496: 01-02 16:07:45.219 8532 8561 E TestRunner: at android.hardware.camera2.CameraManager$CameraManagerGlobal.setTorchMode(CameraManager.java:2933) Line 12497: 01-02 16:07:45.219 8532 8561 E TestRunner: at android.hardware.camera2.CameraManager.setTorchMode(CameraManager.java:1649) Line 12498: 01-02 16:07:45.219 8532 8561 E TestRunner: at android.hardware.camera2.cts.FlashlightTest.resetTorchModeStatus(FlashlightTest.java:420) Line 12542: 01-02 16:07:45.219 8532 8561 E TestRunner: Caused by: android.os.ServiceSpecificException: setTorchMode:3299: Setting torch mode of camera &quot;0&quot; to 0 failed: Broken pipe (-32) (code 10) Line 12547: 01-02 16:07:45.219 8532 8561 E TestRunner: at android.hardware.ICameraService$Stub$Proxy.setTorchMode(ICameraService.java:1311) Line 12548: 01-02 16:07:45.219 8532 8561 E TestRunner: at android.hardware.camera2.CameraManager$CameraManagerGlobal.setTorchMode(CameraManager.java:2930) Line 12610: 01-02 16:07:45.223 1411 7059 E CameraService: setTorchMode: camera id is invalid 0 Line 12612: 01-02 16:07:45.223 3864 4015 E FlashlightController: java.lang.IllegalArgumentException: setTorchMode:3216: Camera ID &quot;0&quot; is a not valid camera ID Line 12614: 01-02 16:07:45.223 3864 4015 E FlashlightController: at android.hardware.camera2.CameraManager$CameraManagerGlobal.setTorchMode(CameraManager.java:2933) Line 12615: 01-02 16:07:45.223 3864 4015 E FlashlightController: at android.hardware.camera2.CameraManager.setTorchMode(CameraManager.java:1649) Line 12622: 01-02 16:07:45.223 3864 4015 E FlashlightController: Caused by: android.os.ServiceSpecificException: setTorchMode:3216: Camera ID &quot;0&quot; is a not valid camera ID (code 3) Line 12627: 01-02 16:07:45.223 3864 4015 E FlashlightController: at android.hardware.ICameraService$Stub$Proxy.setTorchMode(ICameraService.java:1311) Line 12628: 01-02 16:07:45.223 3864 4015 E FlashlightController: at android.hardware.camera2.CameraManager$CameraManagerGlobal.setTorchMode(CameraManager.java:2930) Line 13942: 01-02 16:07:45.914 1864 3648 D TorchManagerService: onTorchModeChanged, mbTorchMode: false, mbDoSetTorchModeFalseByUs: false Line 13947: 01-02 16:07:45.914 3864 4015 I SystemUi--QuickSettings: FlashlightController--&gt;onTorchModeChanged setTorchMode=false cameraId=0
最新发布
06-17
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值