POJ3497 Assemble(二分)
2017-11-22 18:01
232 查看
Link:http://poj.org/problem?id=3497
Assemble
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
Sample Output
Source
Northwestern Europe 2007
AC code:
#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<math.h>
#include<string.h>
#include<map>
#include<set>
#include<vector>
#define LL long long
#define INF 0xfffffff
#define PI acos(-1)
#define EPS 1e-6
using namespace std;
struct node{
int price;
int quality;
};
int n,b,num;
map<string,int>m;
vector<node>v[1010];
bool buy(int m,int b)
{
int cnt=0,i,j,mon,minp;
mon=b;
for(i=1;i<=num;i++)
{
minp=INF;
for(j=0;j<v[i].size();j++)
{
if(v[i][j].quality>=m && v[i][j].price<=minp)
{
minp=v[i][j].price;
}
}
if(minp==INF)
return false;
else
{
mon-=minp;
if(mon<0)
return false;
}
}
return true;
}
int Bsearch(int l,int h)
{
int m,res;
res=0;
while(l<=h)
{
m=(l+h)>>1;
if(buy(m,b))
{
res=m;
l=m+1;
}
else
{
h=m-1;
}
}
return res;
}
int main()
{
// freopen("in.txt","r",stdin);
int t,i,p,q,low,high,ans;
char typ[111],nam[111];
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&b);
m.clear();
num=0;
low=0;
high=0;
for(i=0;i<n;i++)
{
scanf("%s%s%d%d",&typ,&nam,&p,&q);
high=max(high,q);
if(m.find(typ)==m.end())
{
m[typ]=++num;
v[m[typ]].clear();
}
node data;
data.price=p;
data.quality=q;
v[m[typ]].push_back(data);
}
ans=Bsearch(low,high);
printf("%d\n",ans);
}
return 0;
}
Assemble
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 3623 | Accepted: 1144 |
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
Source
Northwestern Europe 2007
AC code:
#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<math.h>
#include<string.h>
#include<map>
#include<set>
#include<vector>
#define LL long long
#define INF 0xfffffff
#define PI acos(-1)
#define EPS 1e-6
using namespace std;
struct node{
int price;
int quality;
};
int n,b,num;
map<string,int>m;
vector<node>v[1010];
bool buy(int m,int b)
{
int cnt=0,i,j,mon,minp;
mon=b;
for(i=1;i<=num;i++)
{
minp=INF;
for(j=0;j<v[i].size();j++)
{
if(v[i][j].quality>=m && v[i][j].price<=minp)
{
minp=v[i][j].price;
}
}
if(minp==INF)
return false;
else
{
mon-=minp;
if(mon<0)
return false;
}
}
return true;
}
int Bsearch(int l,int h)
{
int m,res;
res=0;
while(l<=h)
{
m=(l+h)>>1;
if(buy(m,b))
{
res=m;
l=m+1;
}
else
{
h=m-1;
}
}
return res;
}
int main()
{
// freopen("in.txt","r",stdin);
int t,i,p,q,low,high,ans;
char typ[111],nam[111];
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&b);
m.clear();
num=0;
low=0;
high=0;
for(i=0;i<n;i++)
{
scanf("%s%s%d%d",&typ,&nam,&p,&q);
high=max(high,q);
if(m.find(typ)==m.end())
{
m[typ]=++num;
v[m[typ]].clear();
}
node data;
data.price=p;
data.quality=q;
v[m[typ]].push_back(data);
}
ans=Bsearch(low,high);
printf("%d\n",ans);
}
return 0;
}
相关文章推荐
- 【二分答案】 【POJ3497】 【Northwestern Europe 2007】 Assemble 组装电脑
- 【二分答案】 【POJ3497】 【Northwestern Europe 2007】 Assemble 组装电脑
- POJ3497 UVA12124 Assemble(二分 + 贪心)
- UVA 12124 - Assemble(二分答案)
- UVaLive 3971 Assemble (水题二分+贪心)
- UVALive 3971 Assemble(二分+贪心)
- ZOJ 3090 Assemble(二分+贪心)
- HDU 2333 & POJ 3497 & UVA 12124 Assemble (二分答案)
- NWERC 2007 / UVa 12124 Assemble (二分搜索&最小值最大问题)
- 【二分答案+贪心】解决“最小值最大”问题(UVa 12124 - Assemble)
- uva 12124 - Assemble(二分定界,4级)
- HDU2333 Assemble(二分)
- LA_3635/HDU_2333 Assemble (二分)
- uva 12124 - Assemble(二分定界,4级)
- uvalive 3971 - Assemble(二分搜索 + 贪心)
- hdu 2333 Assemble 二分
- UVA 12124 Assemble (二分 + 字典树)
- UVA ~ 12124 ~ Assemble(map+二分)
- POJ3497二分+贪心
- uvalive 3971 - Assemble(二分搜索 + 贪心)