ACM: 二分题 poj 3497
2016-05-19 23:27
246 查看
[align=center]Assemble
[/align]
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
[/align]
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 题意: 组装一部电脑, 现在要求每样配件选取一个, 使得价钱最小且不超过b价格, 求出最大的品质和. 解题思路: 1. 求解这类"最小值最大"的问题一般方法"二分法". 确定最大和最小的quality, 通过二分 枚举, 判断是否满足最小价钱不超过b价格. 问题自然求解. 2. poj这题数据有点BT, 好多次TLE, 合理使用STL才行. 用map查找配件类型, 875MS时间 不高, 后来网上发现根据quality排序可以在判断是否满足时候提速, 好方法. 代码: #include <cstdio> #include <iostream> #include <cstring> #include <string> #include <vector> #include <map> using namespace std; #define MAX 1005 const int INF = (1<<29); struct node { int price, qu; }good[MAX][MAX]; int n, b; map pos; int count[MAX]; int num; inline int max(int a, int b) { return a > b ? a : b; } inline int min(int a, int b) { return a < b ? a : b; } inline int find(string str) { if( !pos.count(str) ) pos[str] = num++; return pos[str]; } inline bool judge(int qu) { int result = 0; for(int i = 0; i < num; ++i) { int minOne = b+1; for(int j = 0; j < count[i]; ++j) { if( good[i][j].qu >= qu ) minOne = min(minOne, good[i][j].price); } result += minOne; if(result > b) return false; } return true; } int main() { // freopen("input.txt", "r", stdin); int caseNum; scanf("%d", &caseNum); while(caseNum--) { scanf("%d %d", &n, &b); num = 0; memset(count, 0, sizeof(count)); pos.clear(); int left = INF, right = 0; int price, qu; char name[22], type[22]; for(int i = 0; i < n; ++i) { scanf("%s %s %d %d", type, name, &price, &qu); right = max(right, qu); left = min(left, qu); int temp = find(type); good[temp][count[temp]].price = price; good[temp][count[temp]++].qu = qu; }
相关文章推荐
- ACM: uva 11210 &n…
- ACM: 暴力题 poj 2741
- ACM: uva 11464
- ACM: zoj 2714
- ACM: 贪心题 uva 11729
- ACM: 简单题 uva 10881
- ACM: 数学题 uva 11300
- 一步一步搭建客服系统
- Spring ClassPathXmlApplicationContext getBean创建实例
- 【转】国内用户如何加快App Store的访问速度
- ACM: 极其简单题 uva&nb…
- ACM: 搜索题 poj 1…
- ACM: 动态规划题 黑书-…
- ACM: 计算几何题 poj 2066
- ACM: 动态规划题 poj&nb…
- ACM: 华东师范oj 1600&n…
- ACM: 金华赛区题目 水题…
- ACM: 动态规划题 poj&nb…
- ACM: 动态规划题 《黑书…
- ACM: 动态规划题 sicily…