CodeForces - 3B 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 ≤ 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.

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.

Examples
input
Copy
3 2
1 2
2 7
1 3
output
7
2

 

贪心,我的贪心策略是如果空间是奇数,那么就先放进去一个最大的占用1空间的物品

之后把剩余的占用1空间的物品进行排序,之后两两合并成占用2空间的物品

贪心地选取合并后的2空间的物品和原来的2空间的物品,答案即可最优

代码:

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 #define cfread(x) scanf("%I64d",&x)
 4 #define cfout(x) printf("%I64d",x)
 5 #define mian main
 6 #define min(x,y) (x<y?x:y)
 7 #define max(x,y) (x<y?y:x)
 8 #define f(i,p,q,t) for(i=p;i<q;i+=t)
 9 #define MAXN 1100000
10 #define inf 0x3f3f3f3f
11 #define mem(x,t) memset(x,t,sizeof(x));
12 #define T true
13 #define F false
14 #define def -1*inf
15 typedef long long ll;
16 struct Data{
17     int number;//载具对应编号
18     ll v; 
19 };
20 Data a[MAXN];
21 Data b[MAXN];
22 vector<int> zans;
23 bool cmp(const Data& a,const Data& b){
24     return b.v<a.v;
25 }
26 int main(){
27     ll n,v;
28     cfread(n);
29     cfread(v);
30     int num1 = 0;//第一种载具数量 
31     int num2 = 0;//第二种载具数量 
32     for(int i=1;i<=n;i++){
33         ll p;
34         cfread(p);
35         if(p==1LL){
36             ll p;
37             cfread(p);
38             a[num1].v=p;
39             a[num1++].number=i;
40         }
41         else{
42             ll p;
43             cfread(p);
44             b[num2].v=p;
45             b[num2++].number=i;
46         }
47     }
48     sort(a,a+num1,cmp);
49     sort(b,b+num2,cmp);
50     ll ans = 0;
51     int p = 0;
52     int q = 0;
53     if(v%2==1){
54         ans = ans + a[p].v;
55         if(p<num1){
56             zans.push_back(a[p].number);
57             p++;
58         }
59     }
60     while(v>1){//将第一种载具二合一,也叫它占地空间为2,也就是贪心地放占地为2的载具
61          if(p>=num1&&q>=num2)
62             break;
63          ll w1 = a[p].v+a[p+1].v;
64          ll v1 = 2;
65          if(p+1>=num1)
66             v1=1;
67         ll w2=b[q].v;
68         ll v2 = 2;
69         if(w1>w2){
70             ans = ans + w1;
71             v = v - v1;
72             if(p<num1)
73             zans.push_back(a[p].number);
74             if(p+1<num1)
75             zans.push_back(a[p+1].number);
76             p = p + v1;
77         }
78         else{
79             ans = ans + w2;
80             v = v - v2;
81             if(q<num2)
82             zans.push_back(b[q].number);
83             q = q + 1;
84         }
85     }
86     cfout(ans);
87     putchar('\n');
88     for(int i=0;i<zans.size();i++)
89         printf("%d ",zans[i]);
90     return 0;
91 }

 

posted @ 2018-03-08 22:26  晓风微微  阅读(405)  评论(0编辑  收藏  举报