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.

题目大意是有体积为v的背包,有体积为1和2的两种物品若干,这些物品都有各自的价值。求如何取这些物品可使背包中物品的价值最大。

这种题目一看就是0-1背包问题。但是按照传统0-1背包的解法,复杂度是O(nv),在这里也就是10^14,所以肯定是不行的。考虑到这里只有两种物品,所以可以考虑贪心的方法。

方法1:

1.将kayak和catamaran按价值的降序排列。

2.先尽量将catamaran放进ans,记录最后一个catamaran的位置lastc.

3.尽量将kayak放进ans,记录剩下的kayak的起始位置i

4.比较ans[lastc] 和 kayak[i]+kayak[i+1],如果ans[lastc] < kayak[i]+kayak[i+1] 则用这两个kayak替换掉ans[lastc]处的catamaran。然后lastc向前移动一格,i向后移动两格。

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <stack>
#include <map>
#include <algorithm>
#include <vector>
using namespace std;

bool cmp(pair<int, int> p1, pair<int, int> p2) {
    return p2.second < p1.second;
}

int main() {
    vector<pair<int, int> > kays;
    vector<pair<int, int> > cats;
    vector<pair<int, int> > ans;

    int n, v;
    int t, p;
    scanf("%d %d", &n, &v);
    for (int i = 0; i < n; i++) {
        scanf("%d %d", &t, &p);
        if (t == 1) {
            kays.push_back(make_pair(i+1, p));
        } else {
            cats.push_back(make_pair(i+1, p));
        }
    }

    sort(kays.begin(), kays.end(), cmp);
    sort(cats.begin(), cats.end(), cmp);

    int cidx = 0;
    while (v > 1 && cidx < cats.size()) {
        ans.push_back(cats[cidx]);
        v -= 2;
        cidx ++;
    }

    int lastc = ans.size() - 1;
    int kidx = 0;
    while (v > 0 && kidx < kays.size()) {
        ans.push_back(kays[kidx]);
        v -= 1;
        kidx++;
    }

    for (; kidx < kays.size() - 1 && lastc >= 0; kidx+=2) {
        if (kays[kidx].second+ kays[kidx+1].second > ans[lastc].second) {
            ans.erase(ans.begin()+lastc);
            ans.push_back(kays[kidx]);
            ans.push_back(kays[kidx+1]);
            lastc--;
        }
    }

    if (lastc >= 0 && kidx == kays.size() - 1) {
        if (kays[kidx].second > ans[lastc].second) {
            ans.erase(ans.begin()+lastc);
            ans.push_back(kays[kidx]);
            lastc--;
        }
    }

    if (ans.size() > 0) {
        int total = 0;
        for (int i = 0; i < ans.size(); i++) total += ans[i].second;
        printf("%d\n%d", total, ans[0].first);
        for (int i = 1; i < ans.size(); i++) printf(" %d", ans[i].first);
        printf("\n");
    } else {
        printf("0\n");
    }

    return 0;
}

方法2:

1.将体积为1的物品按价值降序排列

2.将体积为2的物品按价值降序排列

3.枚举放入的体积为2的物品数目(设为i),则体积为1的物品数目为v-2*i。记录获得最大价值的数目组合即可。

4.这样的枚举复杂度为O(catamaran's num)

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <stack>
#include <map>
#include <algorithm>
#include <vector>
using namespace std;

bool cmp(pair<int, int> p1, pair<int, int> p2) {
    return p2.second < p1.second;
}

int main() {
    vector<pair<int, int> > kayak;
    vector<pair<int, int> > catamaran;

    int n, v;
    int t, p;
    scanf("%d %d", &n, &v);
    for (int i = 0; i < n; i++) {
        scanf("%d %d", &t, &p);
        if (t == 1) {
            kayak.push_back(make_pair(i+1, p));
        } else {
            catamaran.push_back(make_pair(i+1, p));
        }
    }

    sort(kayak.begin(), kayak.end(), cmp);
    sort(catamaran.begin(), catamaran.end(), cmp);

    vector<int> kval;
    kval.push_back(0);
    for (int i = 0; i < kayak.size(); i++) kval.push_back(kval[kval.size()-1] + kayak[i].second);

    vector<int> cval;
    cval.push_back(0);
    for (int i = 0; i < catamaran.size(); i++) cval.push_back(cval[cval.size()-1] + catamaran[i].second);

    int cnum = 0, knum = 0;
    int ans = 0;
    for (int i = 0; i < cval.size() && i*2 <= v; i++) {
        int j = v-2*i;
        j = (j >= kval.size() ? kval.size() - 1 : (j < 0 ? 0 : j));
        if (cval[i] + kval[j] > ans) {
            ans = cval[i] + kval[j];
            cnum = i;
            knum = j;
        }
    }

    vector<int> ids;
    for (int i = 0; i < knum; i++) ids.push_back(kayak[i].first);
    for (int i = 0; i < cnum; i++) ids.push_back(catamaran[i].first);

    printf("%d\n", ans);
    if (ans != 0) {
        printf("%d", ids[0]);
        for (int i = 1; i < ids.size(); i++) printf(" %d", ids[i]);
        printf("\n");
    }
    return 0;
}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值