CodeForce 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

大致题意:有n件物品和一个容量为v的背包,每件物品都有一个价值,且体积为1或2,问怎样放这些物品可以使背包内物品的价值最大。

分析:

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 <cstdio>
#include <algorithm>
#include <vector>
using namespace std;

bool comp(pair <int, int> x, pair <int, int> y) {
    return x.second > y.second;
}

int main() {
    int n, v;
    while(~scanf("%d%d", &n, &v)) {
        vector < pair <int, int> > kays;
        vector < pair <int, int> > cats;
        vector < pair <int, int> > ans;

        int t, p;
        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(), comp);
        sort(cats.begin(), cats.end(), comp);

        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);  //两个体积为1的换一个体积为2的
                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", 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;
}


你可能感兴趣的:(CodeForce 3B Lorry(贪心))