您的位置:首页 > 其它

解题报告:HDU_2333 Assemble 二分

2016-07-06 14:17 369 查看

Assemble

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 654    Accepted Submission(s): 237


[align=left]Problem Description[/align]
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.

 

[align=left]Input[/align]
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.

 

[align=left]Output[/align]
Per testcase:

One line with one integer: the maximal possible quality.
 

[align=left]Sample Input[/align]

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

 

[align=left]Sample Output[/align]

9

 

[align=left]Source[/align]
Northwestern Europe 2007

 

[align=left]Recommend[/align]
zty
 

Statistic | Submit | Discuss
| Note

题意:
挑电脑配件,已知n个配件类别,价格,性能,问给定价格的最大化的电脑性能。
要求每类配件必须挑一个,电脑的性能为所挑配件里的最低性能。

思路:

二分最低性能,每个配件选符合最低性能的最低价格,如果价格不超过总钱数则满足要求。
将字符串映射成对应的编号:
如果用的map<string,int>,在G++会WA,要交C++。

也可以用数组,但代码量会大一点。

代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<queue>
#include<map>
#include<iostream>

using namespace std;

map<string,int>M;

struct obj{
int pr;
int k;
obj(){}
obj(int a,int b){
pr = a;k =b;
}
}A[1005][1005];
int cnt[1005],m;
int n,sum,price,k;

inline bool cmp(const obj& a,const obj& b){
if(a.k==b.k){
return a.pr<b.pr;
}return a.k>b.k;
}

bool judge(int x){
int res = sum;
for(int i=1;i<=m;i++){//printf("i=%d\n",i);
int id = -1 , p = res;
for(int j=0;j<cnt[i];j++){
if(A[i][j].k>=x){
if(A[i][j].pr<=p){
id = j;
p = A[i][j].pr;
}
}
}if(id==-1){
return false ;
}else {
res -= p;
}
}

return res>=0;
}

inline void print(){
for(int i=1;i<=m;i++){printf("-------i=%d\n",i);
for(int j=0;j<cnt[i];j++){
printf("%d %d \n",A[i][j].pr,A[i][j].k);
}
}
}

int main(){
int T;
scanf("%d",&T);ios::sync_with_stdio(0);
while(T--){ M.clear();
memset(A,0,sizeof(A));
memset(cnt,0,sizeof(cnt));m = 0;
string str,tmp;int s = 0 , e = 0;
n=0,sum=0,price=0,k=0;
cin>>n>>sum;
while(n--){
cin>>str>>tmp>>price>>k;
e = max(k,e);
if(!M.count(str)){
M[str]=++m;
}int t = M[str];

A[t][cnt[t]++]=obj(price,k);

}for(int i=1;i<=m;i++){
sort(A[i],A[i]+cnt[i],cmp);
}//print();
int mid;
while(s<=e){
mid = (s+e)/2;
if(mid==s){
break;
}//printf("mid=%d\n",mid);

if(judge(mid)){
s = mid;
}else {
e = mid - 1;
}
}if(judge(s+1)){
s++;
}

printf("%d\n",s);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: