Assemble
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 3171   Accepted: 1013

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


很久没做的二分答案的题目了,很好的二分答案练手题目。

这种最大中最小,最小中最大一般都是二分答案
然后判断答案的正确与否

代码如下:
#include<cstdio>
#include<cstring>
#include<cstdlib>
using namespace std;
typedef struct node
{
char name[30];
char type[30];
int price;
int quality;
int TYPE;
}node;
node thing[1010];
int flag[1010];
int anslist[1010];
int n,b,cnt,ans;
int cmp(const void *i,const void *j)
{
node *ii=(node *)i,*jj=(node *)j;
return strcmp(ii->type,jj->type);
}
int cmp3(const void *i,const void *j)
{
return *(int *)i-*(int *)j;
}
int OK(int std)
{
int sum=0,PRICE;
for(int i=1;i<=cnt;i++)
{
PRICE=1000001;
for(int j=flag[i];j<flag[i+1];j++)
if(thing[j].quality>=std)
if(thing[j].price<PRICE) PRICE=thing[j].price;
if(PRICE==1000001) return 0;
else sum=sum+PRICE;
}
if(sum<=b) return 1;
else return 0;
}
int getans()
{
int i,j,m;
i=1;j=n;
while(i<j)
{
m=(i+j)/2+1;
if(OK(anslist[m])) i=m;
else j=m-1;
}
ans=i;
}
int main()
{
// freopen("a.in","r",stdin);
// freopen("a.out","w",stdout);
int T;
scanf("%d",&T);
while(T--)
{
cnt=1;
scanf("%d%d\n",&n,&b);
for(int i=1;i<=n;i++)
{
scanf("%s %s %d %d\n",thing[i].type,thing[i].name,&thing[i].price,&thing[i].quality);
anslist[i]=thing[i].quality;
}
qsort(thing+1,n,sizeof(thing[1]),cmp);
qsort(anslist+1,n,sizeof(anslist[1]),cmp3);
flag[cnt]=1;
for(int i=2;i<=n;i++)
{ if(strcmp(thing[i].type,thing[i-1].type)==0) thing[i-1].TYPE=cnt;
else
{
thing[i-1].TYPE=cnt;
cnt++;
flag[cnt]=i;
}
}
flag[cnt+1]=n+1;
thing[n].TYPE=cnt;
getans();
printf("%d\n",anslist[ans]);
}
return 0;
}

附书上解释:

【分析】

在《入门经典》一书中,我们曾提到过,解决“最小值最大”的常用方法是二分答案。假设答案为x,如何判断这个x是最小还是最大呢?删除品质因子小于x的所有配件,如果可以组装出一台不超过b元的电脑,那么标准答案ans≥x,否则ans<x

如何判断是否可以组装出满足预算约束的电脑呢?很简单,每一类配件选择最便宜的一个即可。如果这样选都还超预算的话,就不可能有解了。代码如下。

【二分答案】 【POJ3497】 【Northwestern Europe 2007】 Assemble 组装电脑的更多相关文章

  1. 【二分答案】【POJ3122】【Northwestern Europe 2006】Pie

    Pie Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10629   Accepted: 3744   Special Ju ...

  2. 【二分答案+贪心】解决“最小值最大”问题(UVa 12124 - Assemble)

    Problem A - Assemble Time limit: 2 seconds Recently your team noticed that the computer you use to p ...

  3. UVA 12124 Assemble(二分答案)

    题目链接:https://vjudge.net/problem/UVA-12124 垃圾vjudge毁我青春!! 首先这道题是解决“最小值最大”的问题,所以要二分答案. 在这里我们二分$quality ...

  4. [置顶] Ants(Northeastern Europe 2007)

                                                                                      Ants Time Limit: 5 ...

  5. 【二分答案】【哈希表】【字符串哈希】bzoj2946 [Poi2000]公共串

    二分答案,然后搞出hash值扔到哈希表里.期望复杂度O(n*log(n)). <法一>next数组版哈希表 #include<cstdio> #include<cstri ...

  6. 二分答案:Poweroj2461-入门基础之二分答案(二分法的应用)

    传送门:点击打开链接 入门基础之二分答案 Time Limit: 1000 MS Memory Limit: 65536 KBTotal Submit: 179 Accepted: 33 Page V ...

  7. CH Round #72树洞[二分答案 DFS&&BFS]

    树洞 CH Round #72 - NOIP夏季划水赛 描述 在一片栖息地上有N棵树,每棵树下住着一只兔子,有M条路径连接这些树.更特殊地是,只有一棵树有3条或更多的路径与它相连,其它的树只有1条或2 ...

  8. [CF752E]Santa Claus and Tangerines(二分答案,dp)

    题目链接:http://codeforces.com/contest/752/problem/E 题意:给n个橘子,每个橘子a(i)片,要分给k个人,问每个人最多分多少片.每个橘子每次对半分,偶数的话 ...

  9. [NOIP2011] 聪明的质检员(二分答案)

    题目描述 小T 是一名质量监督员,最近负责检验一批矿产的质量.这批矿产共有 n 个矿石,从 1到n 逐一编号,每个矿石都有自己的重量 wi 以及价值vi .检验矿产的流程是: 1 .给定m 个区间[L ...

随机推荐

  1. Ubuntu12.04 Git 服务器详细配置

    Git是一款免费.开源的分布式版本控制系统,用于敏捷高效地处理任何或小或大的项目,学过Linux的都知道,Git的优点我就不再多说了,我也是很喜欢Linux的.今天我们一起学习Git服务器在Ubunt ...

  2. JSON 学习笔记

    学习使用json过程随笔: json数组格式 var employees = [ { "firstName":"Bill" , "lastName&q ...

  3. gradient杂谈

    工作中难免遇到某些小项目中没有设计的情况,这对于PS基础薄弱的我来说非常恐怖.无奈之下,只好自己自学UI方面的知识,但对于某些能用CSS实现的背景样式等,还是尽可能地用已经掌握的知识去实现.本文主要分 ...

  4. 前端--关于CSS盒模型

    CSS样式规则的学习是很繁琐和枯燥的,因为它不像物理.数学或者其他编程语言一样有一些基本概念.有一些基本公理或者规则,其他所有的表现都是概念在这些公里或者规则之下的逻辑游戏,CSS是有一些基本概念,但 ...

  5. js中的eval方法转换对象时,为何一定要加上括号?

    待转换的是一个Json字符串: {'name':'新欢'} 而使用如下这种方式调用则会抛出语法异常, eval("{'name':'新欢'}"); 必须加上括号才行 eval(&q ...

  6. oracle11gr2 netca 无法启动 报错

    安装oracle软件后,必须要先配置listener才能dbca建库,但是netca却报下面的错误. Oracle Net Services Configuration: # # An unexpec ...

  7. 修改 “嗨加游-Prefix.pch” 或者 “嗨加游-Info.plist ” 方法

    如果要更改Info.plist与Prefix.pch文件实际路径,也就是实际文件的位置(不是在工程中的组织路径),需要到Build Settings中修改对应的配置,不然工程就找不到对应的Info.p ...

  8. uva 10935 throwing cards away <queue>

    Given is an ordered deck of    n    cards numbered 1 to    n    with card 1 at the top and card    n ...

  9. (转)JAVA路径问题及命令行编译运行基础(linux下)

    原地址: http://blog.csdn.net/biaobiaoqi/article/details/6846274 java的运行机制的基本概念: 源文件 也就是我们熟知的.java文件. 类文 ...

  10. Ecstore中如何调用发起Ajax请求

    Ecstore的JS框架使用了mootools,所以ajax调用也使用mootools中的Request组件. 语法: var myRequest = new Request([options]); ...