Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 3171 | Accepted: 1013 |
Descriptionnode
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.git
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.less
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.ide
Inputidea
On the first line one positive number: the number of testcases, at most 100. After that per testcase:spa
One line with two integers: 1 ≤ n ≤
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 ≤
Outputorm
Per testcase:ip
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。
如何判斷是否能夠組裝出知足預算約束的電腦呢?很簡單,每一類配件選擇最便宜的一個便可。若是這樣選都還超預算的話,就不可能有解了。代碼以下。