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.
3 2 1 2 2 7 1 3
7 2
题目大意:
给n个商品,但是商品有两种属性,分别是代价和价值。但是只有两种代价,1或者2。
解题思路&反省:
由于数据范围的限制,我们不能使用背包方法(n*v)来求解。
一开始的第一个想法是对于每一个商品都处理出一个第三属性,就是性价比= =然后直接选取性价比最大的。
如果这么做能行的话,背包也可以这么做了。因为会涉及到一个类似于最优匹配的问题,
原因是优先选取性价比最高的商品,有可能会导致空出很多空间使得剩下的商品不能放置,那么可能选取性价比较低的商品会更好
正确的方法是,将商品数据按代价1/2分两组存储。对于每一组,由于代价相同,必然会优先选择价值高的。
而两组商品与空间的匹配问题,可以通过枚举两组商品的个数来解决,但是这样会超时。
但是很明显,可以多塞东西必然多塞是最好的,所以可以仅仅枚举其中一个商品的个数,另一个用容量来减。
WA原因:
(1)看代码中的注释
(2)用容量来减的时候,会出现总容量大于所有商品空间的情况,这个时候减了会错,边界情况没处理好
下面是ac代码:
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <memory.h>
#include <string>
#include <vector>
#include <list>
#include <map>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <numeric>
#include <functional>
#define maxn 100005
using namespace std;
struct node{
int cap;
int num;
};
node a1[maxn],a2[maxn];
int n,v;
int cmp(node a,node b)
{
return b.cap<a.cap;
}
int main()
{
int maxt=0,maxv=0;
scanf("%d %d",&n,&v);
int i1,i2,i;
for(i1=1,i2=1,i=1;i<=n;i+=1){
int t,p;
scanf("%d %d",&t,&p);
maxt+=t,maxv+=p;
if(t==1) a1[i1].cap=p,a1[i1++].num=i;
else a2[i2].cap=p,a2[i2++].num=i;
}
if(maxt<=v){
printf("%d\n",maxv);
for(int i=1;i<=n;i+=1) printf("%d ",i);
printf("\n");
return 0;
}
a1[0].cap=0,a1[0].num=0;
a2[0].cap=0,a2[0].num=0;
sort(a1+1,a1+i1,cmp);
sort(a2+1,a2+i2,cmp);
for(int i=1;i<i1;i+=1) a1[i].cap=a1[i-1].cap+a1[i].cap;
for(int i=1;i<i2;i+=1) a2[i].cap=a2[i-1].cap+a2[i].cap;
int ans=0,ansi=0,ansj=0;
for(int i=0;i<i2&&i*2<=v;i+=1){
int j=v-2*i;
if(j<i1&&ans<a2[i].cap+a1[j].cap){
//这里有一个错,当v为偶数/奇数时,j一定是偶数/奇数
ans=a2[i].cap+a1[j].cap;
ansi=i;
ansj=j;
}
if(j-1&&j-1<i1&&ans<a2[i].cap+a1[j-1].cap){
//也就是说不能同时遍历所有的奇数偶数,所以这里-1
ans=a2[i].cap+a1[j-1].cap;
ansi=i;
ansj=j-1;
}
}
if(i1==1)
for(int i=i2;i>=1;i-=1)
if(i*2<=v){
ans=a2[i].cap;
ansi=i;
ansj=0;
break;
}
if(i2==1)
for(int i=i1;i>=1;i-=1)
if(i<=v){
ans=a1[i].cap;
ansi=0;
ansj=i;
break;
}
printf("%d\n",ans);
for(int i=1;i<=ansi;i+=1){
printf("%d",a2[i].num);
if(i==ansi&&!ansj) printf("\n");
else printf(" ");
}
for(int j=1;j<=ansj;j+=1){
printf("%d",a1[j].num);
if(j==ansj) printf("\n");
else printf(" ");
}
return 0;
}
/*
10 10
2 14
1 100
2 11
2 12
2 9
2 14
2 15
2 9
2 11
2 6
*/