POJ3497二分+贪心

Assemble
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 2578   Accepted: 837

Description

Recently your team noticed that the computer you use to practice for programming contests is not good enough anymore. Therefore, you decide to buy a new computer.

To make the ideal computer for your needs, you decide to buy separate components and assemble the computer yourself. You need to buy exactly one of each type of component.

The problem is which components to buy. As you all know, the quality of a computer is equal to the quality of its weakest component. Therefore, you want to maximize the quality of the component with the lowest quality, while not exceeding your budget.

Input

On the first line one positive number: the number of testcases, at most 100. After that per testcase:

  • One line with two integers: 1 ≤ n ≤ 1 000, the number of available components and 1 ≤ b ≤1 000 000 000, your budget.

  • n lines in the following format: “type name price quality”, where type is a string with the type of the component, name is a string with the unique name of the component, price is an integer (0 ≤ price ≤ 1 000 000) which represents the price of the component and quality is an integer (0 ≤ quality ≤1 000 000 000) which represents the quality of the component (higher is better). The strings contain only letters, digits and underscores and have a maximal length of 20 characters.

Output

Per testcase:

  • One line with one integer: the maximal possible quality.

Sample Input

1
18 800
processor 3500_MHz 66 5
processor 4200_MHz 103 7
processor 5000_MHz 156 9
processor 6000_MHz 219 12
memory 1_GB 35 3
memory 2_GB 88 6
memory 4_GB 170 12
mainbord all_onboard 52 10
harddisk 250_GB 54 10
harddisk 500_FB 99 12
casing midi 36 10
monitor 17_inch 157 5
monitor 19_inch 175 7
monitor 20_inch 210 9
monitor 22_inch 293 12
mouse cordless_optical 18 12
mouse microsoft 30 9
keyboard office 4 10

Sample Output

9
思路:装机问题,你要装一台电脑,电脑由多个配件组成,给定配件类型,名字,价格,质量等级。在保证预算不超过的前提下求使得电脑质量等级尽可能高的方案。
一个电脑的质量等级由质量最低的那个配件等级所决定。将配件按类型排序,然后二分配件等级,同一个类型的选择价格较低的,由此计算总价格。
View Code
 1 #include <cstdio>
 2 #include <cmath>
 3 #include <cstring>
 4 #include <iostream>
 5 #include <algorithm>
 6 using namespace std;
 7 
 8 #define inf 2000000000
 9 struct node
10 {
11     int a,b;
12     char c[100];
13 }p[1010];
14 int kind[1010],top,money;
15 bool cmp(node M,node N)
16 {
17     if(strcmp(M.c,N.c)!=0)
18     return strcmp(M.c,N.c)<0;
19     return M.b>N.b;
20 }
21 bool check(int u)
22 {
23     int res=0;
24     for(int i=1;i<=top;i++)
25     {
26         int _min=inf;
27         for(int j=kind[i-1]+1;j<=kind[i];j++)
28         {
29             if(u<=p[j].b)
30             {
31                 _min=min(_min,p[j].a);
32             }
33             else break;
34         }
35         if(_min==inf) return 0;
36         res+=_min;
37     }
38     if(res>money) return 0;
39     return 1;
40 }
41 int binary(int left,int right)
42 {
43     while(left<=right)
44     {
45         int mid=(left+right)>>1;
46         if(check(mid))
47         {
48             left=mid+1;
49         }
50         else
51         {
52             right=mid-1;
53         }
54     }
55     return right;
56 }
57 int main()
58 {
59     int t,n;
60     char yy[100];
61     scanf("%d",&t);
62     while(t--)
63     {
64         scanf("%d%d",&n,&money);
65         int _min=inf,_max=0;
66         for(int i=1;i<=n;i++)
67         {
68             scanf("%s %s %d %d",p[i].c,yy,&p[i].a,&p[i].b);
69             _min=min(_min,p[i].b);
70             _max=max(_max,p[i].b);
71         }
72         sort(p+1,p+n+1,cmp);
73         top=0;
74         memset(kind,0,sizeof(0));
75         for(int i=1;i<=n-1;i++)
76         {
77             if(strcmp(p[i].c,p[i+1].c)!=0)
78             kind[++top]=i;
79         }
80         kind[++top]=n;
81         printf("%d\n",binary(_min,_max));
82     }
83 }

 

posted @ 2012-08-05 22:09  _sunshine  阅读(496)  评论(0编辑  收藏  举报