codeforce3B

本文介绍了一种针对特定场景的最优装载策略算法,旨在最大化装载货物的总承载能力。该算法适用于两类不同大小的水上交通工具(皮划艇和双体船),在限定的空间内找到最佳装载组合。

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

A group of tourists is going to kayak and catamaran tour. A rented lorry has arrived to the boat depot to take kayaks and catamarans to the point of departure. It's known that all kayaks are of the same size (and each of them occupies the space of 1 cubic metre), and all catamarans are of the same size, but two times bigger than kayaks (and occupy the space of 2 cubic metres).

Each waterborne vehicle has a particular carrying capacity, and it should be noted that waterborne vehicles that look the same can have different carrying capacities. Knowing the truck body volume and the list of waterborne vehicles in the boat depot (for each one its type and carrying capacity are known), find out such set of vehicles that can be taken in the lorry, and that has the maximum total carrying capacity. The truck body volume of the lorry can be used effectively, that is to say you can always put into the lorry a waterborne vehicle that occupies the space not exceeding the free space left in the truck body.

Input

The first line contains a pair of integer numbers n and v (1 ≤ n ≤ 1051 ≤ v ≤ 109), where n is the number of waterborne vehicles in the boat depot, and v is the truck body volume of the lorry in cubic metres. The following n lines contain the information about the waterborne vehicles, that is a pair of numbers ti, pi (1 ≤ ti ≤ 21 ≤ pi ≤ 104), where ti is the vehicle type (1 – a kayak, 2 – a catamaran), and pi is its carrying capacity. The waterborne vehicles are enumerated in order of their appearance in the input file.

Output

In the first line print the maximum possible carrying capacity of the set. In the second line print a string consisting of the numbers of the vehicles that make the optimal set. If the answer is not unique, print any of them.

Example
Input
3 2
1 2
2 7
1 3
Output
7
2
/*题意:一个固定大小的背包,给你体积只为1,2的物品,输出最大的价值并打印用了第几个物品下标*/
/*思路:将两种体积分别排序,将体积为2的先加入数组,最终要么剩余体积为0,要么为1。那么只需要将最后一个体积为2的下标记录,并判断2个体积为1的是否大于当前下标的体积为2的物品价值,是就将体积为2的原来的物品删掉,将两个体积为1的物品加入。有个特例需要特判:如果最终剩余一个体积为1的物品他有可能比当前下标体积为2的物品价值大,那么这种情况也要交换。*/

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>

using namespace std;

bool cmp(pair <int, int> a, pair <int, int> b)
{
    return a.second > b.second;
}

int main()
{
    int n, v;
    while(scanf("%d%d", &n, &v) != EOF){
        vector < pair <int, int> >v1;
        vector < pair <int, int> >v2;
        vector < pair <int, int> >ans;
        int a, b;
        for(int i = 0; i < n; i++){
            scanf("%d%d", &a, &b);
            if(a == 1)
                v1.push_back(make_pair(i + 1, b));  //这里直接对下标加1直接输出
            else
                v2.push_back(make_pair(i + 1, b));
        }
        sort(v1.begin(), v1.end(), cmp);
        sort(v2.begin(), v2.end(), cmp);
        int id2 = 0;
        while(v > 1 && v2.size() > id2){    //先加载体积为2的在加载体积为1的,因为要么刚好全部装的是体积为2的,剩下的为1,要么剩下的为0
            ans.push_back(v2[id2]);
            id2++;
            v -= 2;
        }
        int id1 = 0;
        int lans = ans.size() - 1;  //这里注意记录最后一个体积为2的下标
        while(v > 0 && v1.size() > id1){
            ans.push_back(v1[id1]);
            id1++;
            v -= 1;
        }
        for(;id1 < v1.size() - 1 && lans >= 0; id1 += 2){
            if(v1[id1].second + v1[id1+1].second > ans[lans].second){
                ans.erase(ans.begin() + lans);
                ans.push_back(v1[id1]);
                ans.push_back(v1[id1+1]);
                lans--;
            }
        }
        if(lans >= 0 && v1.size() - 1 == id1){  //这里需要注意可能最后剩下一个体积为1的但是他的价值比已经加入的体积为2的价值大继续交换
            if(v1[v1.size()-1].second > ans[lans].second){
                ans.erase(ans.begin() + lans);
                ans.push_back(v1[v1.size()-1]);
                lans--;
            }
        }
        int total = 0;
        if(ans.size() > 0){
            for(int i = 0; i < ans.size(); i++){
                total += ans[i].second;
            }
            printf("%d\n", total);
            for(int i = 0; i < ans.size() - 1; i++)
                printf("%d ", ans[i].first);
            printf("%d\n", ans[ans.size()-1].first);
        }
        else
            printf("0\n");
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值