NBUT-2014暑期集训专题练习1 -> 二分法 N - N
2014-08-05 10:43
246 查看
DescriptionRecently 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.
InputOn 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.
OutputPer testcase:
One line with one integer: the maximal possible quality.
二分题,既要最大化最小值,也要让价格尽量小,那么在二分答案的时候,选择的当然是满足当前mid的,价格又是最小的组件了,一开始自己用个struct来保存,最后超时了,然后换成vector 的做法, 就能过了。547ms.....
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.
InputOn 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.
OutputPer testcase:
One line with one integer: the maximal possible quality.
二分题,既要最大化最小值,也要让价格尽量小,那么在二分答案的时候,选择的当然是满足当前mid的,价格又是最小的组件了,一开始自己用个struct来保存,最后超时了,然后换成vector 的做法, 就能过了。547ms.....
#include<stdio.h> #include<string.h> #include<iostream> #include<map> #include<vector> #include<algorithm> using namespace std; vector<int>price[1010]; vector<int>quality[1010]; int main() { int t,n,buget; while(~scanf("%d",&t)) { while(t--) { scanf("%d%d",&n,&buget); int i,j; map<string,int>zujian; zujian.clear(); char str1[22],str2[22]; int cnt=1,a,b; int maxn=-1,mins=0x3f3f3f3f; for(i=0;i<n;i++) { scanf("%s%s%d%d",str1,str2,&a,&b); if(zujian[str1]==0)//新组件 { zujian[str1]=cnt; price[cnt].clear(); quality[cnt].clear(); price[cnt].push_back(a); quality[cnt].push_back(b); if(maxn<b) maxn=b; if(mins>b) mins=b; cnt++; } else { price[zujian[str1]].push_back(a); quality[zujian[str1]].push_back(b); if(maxn<b) maxn=b; if(mins>b) mins=b; } } int l=mins,r=maxn,mid; bool flag; while(l<r) { mid=(l+r+1)>>1; int all; i=1; int money=0; bool flag=false; while(i<cnt) { all=0x3f3f3f3f; //printf("%d\n",cen[i]); for(j=0;j<quality[i].size();j++) if(quality[i][j]>=mid) all=min(all,price[i][j]); // printf("%d\n",all); if(all==0x3f3f3f3f) { flag=true; break; } money+=all; i++; } // printf("%d %d %d %d %d %d\n",l,r,mid,money,buget,flag); if(flag || money>buget) r=mid-1; else l=mid; } printf("%d\n",l); } } return 0; }
相关文章推荐
- NBUT-2014暑期集训专题练习1 -> 二分法 A题
- NBUT-2014暑期集训专题练习1 -> 二分法F - F
- NBUT-2014暑期集训专题练习1 -> 二分法 H - H
- NBUT-2014暑期集训专题练习1 -> 二分法E - E
- NBUT-2014暑期集训专题练习1 -> 二分法L - L
- NBUT-2014暑期集训专题练习1 -> 二分法 J - J
- NBUT-2014暑期集训专题练习1 -> 二分法B - B
- 黑马程序员->集合类 (3 代码练习
- Java 基础一些代码练习笔记( GenericFoo<T> 泛型)
- 2013 -> 2014
- 黑马程序员------<补>String 综合练习
- 黑马程序员_<<TCP练习>>
- 黑马程序员_<<String练习>>
- 2014蓝桥杯本科C/C++组预赛第9题<斐波那契>解题报告
- 2014 UESTC暑前集训图论专题解题报告
- 【笔记】2014-01<—>2014-02
- spark 官方动手练习二:用spark浏览维基百科的数据<转>
- 2010暑期集训第二专题(搜索)关于搜索的一点两点三点...
- [直播]2014 PKU ACM暑期集训
- 暑期实训第二课(Arduino上的环境数据采集终端制作<传感器的选择>)