codeforce 3B. Lorry(贪心)

题目:http://codeforces.com/problemset/problem/3/B

大意是,给定N和V,在给出N个vehicle的类型号(1或2)和各自的载重量,求在不超过V的情况下最大的载重值及其分配方案。

这应该不用01背包,如果用背包Vei数组开不了那么大。那怎么破?因为存在物品空间占用1这一特殊条件,所以只要ka够用就能把V填满,这里可以用贪心做。先把ka和ca各自的数据按照"性价比"(也就是载重量)从大到小排序(V=1的ka在这里扮演着修补的角色,所以我们先装ca再装ka,即先多占用空间)。当ka装完或者空间不能继续装下去的时候即停下来,然后装入ca。先把空间使用完再优化,如果ca的空间利用值高则用它来换掉已装空间值低的ka(用尽可能高的换取尽可能低的,让整个空间的利用率快速增长)。按照这个贪心的思路最终能得到结果。(参考了别人的pair代码,我也学习着用用~)

#include <iostream>
#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;
const int maxn=1e5+10;
int n,v;
bool cmp(pair<int,int> a,pair<int,int> b){
    return a.second>b.second;
}
int main()
{
    //freopen("cin.txt","r",stdin);
    while(cin>>n>>v){
        vector<pair<int,int> > ka,ca,ans;
        int i,j;
        int a,b,lasta,firstb,val=0;
        for(i=0;i<n;i++){
            scanf("%d%d",&a,&b);
            if(a&1)ka.push_back(make_pair(i+1,b));
            else ca.push_back(make_pair(i+1,b));
        }
        sort(ka.begin(),ka.end(),cmp);
        sort(ca.begin(),ca.end(),cmp);
        lasta=firstb=0;
        while(v>1&&lasta<ca.size()){
            ans.push_back(ca[lasta++]);
            v-=2;
        }
        while(v>0&&firstb<ka.size()){
            ans.push_back(ka[firstb++]);
            v--;
        }
        i=lasta-1;    j=firstb;
        while(j+1<ka.size()&&i>=0){
            if(ka[j].second+ka[j+1].second>ans[i].second){
                ans.erase(ans.begin()+i);
                ans.push_back(ka[j]);
                ans.push_back(ka[j+1]);
                j++;
                i--;
            }
            j++;
        }
        if(j==ka.size()-1&&i>=0)
            if(ka[j].second>ans[i].second){
                ans.erase(ans.begin()+i);
                ans.push_back(ka[j]);
            }
        for(j=0;j<ans.size();j++){
            val+=ans[j].second;
        }
        if(val){
            printf("%d\n",val);
            for(int q=0;q<ans.size()-1;q++){
                printf("%d ",ans[q].first);
            }
            printf("%d\n",ans[ans.size()-1].first);
        }
        else printf("0\n");
    }
    return 0;
}

题目中有这样一句话:"In the second line print a string consisting of the numbers of the vehicles that make the optimal set." 开始我一直认为是输出type:0或1,但是一直死在test 2,后来看了别人的代码才知道是编号1,2,3,4,……

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值