CF_3B_Lorry

47 篇文章 0 订阅
13 篇文章 0 订阅

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.

Examples
input
3 2
1 2
2 7
1 3
output
7
2


题意有一个卡车的容积为v

有两种船分别给出体积1或者2的物品

船共n个

问卡车拉的最大价值。以及这个组合中有哪些船


这个题目标注是贪心

但无非是体积相同的从价值大的开始拿

有点dp的意思但是注意到体积只有两个

而且数据范围不允许dp

于是只需要列举可能的体积为2的船的数量就可以了

过程中有许多细节需要处理

如全是2没有1的情况 但总空间是1

如装不满的时候

或者单独用1 或者2装不满的时候

另外要用longlong


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

const int M=100005;
struct Node
{
    int v,id;
    bool operator<(Node a)const
    {
        return a.v<v;
    }
}vone[M],vtwo[M];
int so[M],st[M];

int main()
{
    int n,v;
    scanf("%d%d",&n,&v);
    int none=0,ntwo=0;
    for(int i=1;i<=n;i++)
    {
        int con;
        scanf("%d",&con);
        if(con==1)
        {
            vone[none].id=i;
            scanf("%d",&vone[none++].v);
        }
        else
        {
            vtwo[ntwo].id=i;
            scanf("%d",&vtwo[ntwo++].v);
        }
    }
    if(none==0&&v<2)
    {
        printf("0\n\n");
        return 0;
    }
    sort(vone,vone+none);
    sort(vtwo,vtwo+ntwo);
    for(int i=0;i<none;i++)
        so[i+1]=so[i]+vone[i].v;
    for(int i=0;i<ntwo;i++)
        st[i+1]=st[i]+vtwo[i].v;
//    for(int i=0;i<ntwo;i++)
//        cout<<vtwo[i].v<<" "<<vtwo[i].id<<endl;
//    cout<<endl;
//    for(int i=0;i<none;i++)
//        cout<<vone[i].v<<" "<<vone[i].id<<endl;
//    cout<<endl;
    int now=v,ans=none+ntwo,ma=0;
    int i=0;
    if(v>none)
    {
        i=(v-none+1)/2;
        now=none;
        if((v&1)^(none&1))
            now--;
    }
    //cout<<i<<endl;
    for(;i<=min(ntwo,v/2);i++)
    {
        if(now<0)
            break;
        if(so[now]+st[i]>ma)
        {
            ma=so[now]+st[i];
            ans=now;
        }
        now-=2;
    }
    if(ma==0)        //没装满的情况
        ma=so[none]+st[ntwo];
    printf("%d\n",ma);
    //cout<<ans;
    for(int i=0;i<min(none,ans);i++)
        printf("%d ",vone[i].id);
    for(int i=0;i<min(ntwo,(v-ans)/2);i++)
        printf("%d ",vtwo[i].id);
    printf("\n");
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值