您的位置:首页 > 其它

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.....
#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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: