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.
The first line contains a pair of integer numbers n and v (1 ≤ n ≤ 105; 1 ≤ 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 ≤ 2; 1 ≤ 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.
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.
大致思路:对catamarans、kayaks降序排序,往truck中先放catamarans,再放kayaks,然后替换truck中价值不如剩下的kayaks的catamarans。注意可能没的放(v = 1, 全部都是catamarans)
#include<iostream>
#include<fstream>
#include<string>
#include<queue>
#include<stack>
#include<set>
#include<map>
#include<set>
#include<list>
#include<algorithm>
#include<math.h>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<ctime>
#include<iomanip>
using namespace std;
typedef struct
{
int index;
int p;
}node;
bool cmp(node a, node b)
{
return a.p > b.p;
}
node a1[100005], a2[100005], s[100005];
int main()
{
int n, v;
scanf("%d %d", &n, &v);
int t;
int t1 = 0, t2 = 0, t3 = 0;
for(int i = 1; i <= n; ++i)
{
scanf("%d", &t);
if(1 == t)
{
a1[t1].index = i;
scanf("%d", &a1[t1].p);
++t1;
}
else
{
a2[t2].index = i;
scanf("%d", &a2[t2].p);
++t2;
}
}
sort(a1, a1 + t1, cmp);
sort(a2, a2 + t2, cmp);
int result = 0;
int i1 = 0, i2 = 0;
for(; i2 < t2; ++i2)
{
if(v < 2)
break;
s[t3++] = a2[i2];
v -= 2;
}
for(; i1 < t1; ++i1)
{
if(0 == v)
break;
s[t3++] = a1[i1];
--v;
}
--i2;
while(i2 >= 0 && i1 < t1)
{
if(i1 + 1 == t1)
{
if(a1[i1].p > a2[i2].p)
s[i2] = a1[i1];
}
else
{
if(a1[i1].p + a1[i1 + 1].p > a2[i2].p)
{
s[i2] = a1[i1];
s[t3++] = a1[i1 + 1];
}
}
i1 += 2;
--i2;
}
for(int i = 0; i < t3; ++i)
result += s[i].p;
printf("%d\n", result);
for(int i = 0; i < t3 - 1; ++i)
printf("%d ", s[i].index);
if(t3 > 0)
printf("%d\n", s[t3 - 1].index);
return 0;
}