Codeforces 3B. Lorry

B. Lorry
time limit per test
2 seconds
memory limit per test
64 megabytes
input
standard input
output
standard output

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.

Sample test(s)
input
3 2
1 2
2 7
1 3
output
7
2


题意:特殊的背包问题, 背包的容量是v, 有两张可以选择的物品,体积1的物品有x件, 体积2的物品有y件, 体积相同的物品的能力值并不相同, 问你该怎么装, 使的总能力值最大。

思路:贪心的思路,将体积1和2的物品分别按照能力值大小排序,  首先将体积是2的物品按序装到背包, 然后将体积是1的物品按序装到背包, 如果背包满了, 还有体积是1的剩余, 则将体积1的两个组合或者1个(只剩一个了)从后往前替换体积为2的物体(能力值要比体积为2的大才替换)

代码:

#include <stdio.h>
#include <algorithm>
#include <vector>
using namespace std;

const int MAXN = 100005;


struct Vehicle
{
    int id, cap;
    bool operator<(const Vehicle &v) const
    {
        return cap > v.cap;
    }
    Vehicle(){}
    Vehicle(int x, int y) : id(x), cap(y){}
};

int cap[MAXN];
vector<Vehicle> veh1;
vector<Vehicle> veh2;

int main()
{
#ifdef _LOCAL
    freopen("F://input.txt", "r", stdin);
#endif
    int n, v;
    scanf("%d%d", &n, &v);
    for(int i = 1; i <= n; ++i)
    {
        int a, b;
        scanf("%d%d", &a, &b);
        cap[i] = b;
        if(a == 1)
            veh1.push_back(Vehicle(i, b));
        else
            veh2.push_back(Vehicle(i, b));
    }
    sort(veh1.begin(), veh1.end());
    sort(veh2.begin(), veh2.end());
    vector<int> ans;
    int totalCap = 0;
    for(int i = 0; i < veh2.size() && v >= 2; ++i, v -= 2)
    {
        totalCap += veh2[i].cap;
        ans.push_back(veh2[i].id);
    }
    int endFlag = ans.size() - 1;
    int startFlag = 0;
    for( ; startFlag < veh1.size() && v; ++startFlag, --v)
    {
        ans.push_back(veh1[startFlag].id);
        totalCap += veh1[startFlag].cap;
    }
    for(int i = endFlag, j = startFlag; i >= 0 && j < veh1.size(); --i, ++j)
    {
        if(j + 1 < veh1.size())
        {
            if(cap[ans[i]] >= veh1[j].cap + veh1[j + 1].cap)
                break;
            totalCap = totalCap - cap[ans[i]] + veh1[j].cap + veh1[j + 1].cap;
            ans[i] = veh1[j].id;
            ans.push_back(veh1[j + 1].id);
            j = j + 1;
        }
        else  if(cap[ans[i]] < veh1[j].cap)
        {
            totalCap = totalCap - cap[ans[i]] + veh1[j].cap;
            ans[i] = veh1[j].id;
        }
    }
    printf("%d\n", totalCap);
    for(int i = 0; i < ans.size(); ++i)
        printf("%d ", ans[i]);
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Codeforces Round 894 (Div. 3) 是一个Codeforces举办的比赛,是第894轮的Div. 3级别比赛。它包含了一系列题目,其中包括题目E. Kolya and Movie Theatre。 根据题目描述,E. Kolya and Movie Theatre问题要求我们给定两个字符串,通过三种操作来让字符串a等于字符串b。这三种操作分别为:交换a中相同位置的字符、交换a中对称位置的字符、交换b中对称位置的字符。我们需要先进行一次预处理,替换a中的字符,然后进行上述三种操作,最终得到a等于b的结果。我们需要计算预处理操作的次数。 根据引用的讨论,当且仅当b[i]==b[n-i-1]时,如果a[i]!=a[n-i-1],需要进行一次操作;否则不需要操作。所以我们可以遍历字符串b的前半部分,判断对应位置的字符是否与后半部分对称,并统计需要进行操作的次数。 以上就是Codeforces Round 894 (Div. 3)的简要说明和题目E. Kolya and Movie Theatre的要求。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [Codeforces Round #498 (Div. 3) (A+B+C+D+E+F)](https://blog.csdn.net/qq_46030630/article/details/108804114)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [Codeforces Round 894 (Div. 3)A~E题解](https://blog.csdn.net/gyeolhada/article/details/132491891)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值