【UVA12124】Assemble

题面

  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.

题意

  有 n 个物品,每个物品有四个属性:种类Type,名称 Name ,价格 P 和品质Q
  现在要在每一种物品中各选出一个,满足 kPkB ,设其中 Q 的最小值为x,求所有方案中最大的 x
  100组数据,n1000B109P106Q109

解法

二分:
  话说这道题是一个字符串处理题……
  看到最小值最大,很容易想到二分:
  上界 R=Qmax ,下界 L=0 ,对于中值 lim ,在每一种物品中删去 Q 值小于lim的部分,然后在剩下的部分中找到 P 值最小的一个,累加起来,判断与B的关系即可
  现在关键就是找出有多少种物品和每一种物品有多少个:
  这里使用 map 来记录种类,用 vector 来记录编号,详细过程看代码……所以说这一题就是一个字符串处理题……

复杂度

O( nlogR

代码

#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<vector>
#include<map>
#define Lint long long int
using namespace std;
const int INF=1e9+7;
const int MAXN=1010;
struct node
{
    Lint p,q;
};
int T,n,cnt;
Lint b;
map<string,int> id;
vector<node> t[MAXN];
bool check(Lint lim)
{
    Lint ret=0,tmp;
    for(int i=1,x;i<=cnt;i++)
    {
        x=t[i].size()-1,tmp=INF;
        for(int j=0;j<=x;j++)
            if( t[i][j].q>=lim )   tmp=min( tmp,t[i][j].p );
        ret+=tmp;
    }
    return ret<=b;
}
int main()
{
    string type,name;
    Lint x,y,l,r;
    scanf("%d",&T);
    while( T-- )
    {
        l=r=cnt=0;
        scanf("%d%lld",&n,&b);
        for(int i=1;i<=n;i++)
        {
            cin>>type>>name;
            scanf("%lld%lld",&x,&y);
            if( !id[type] )   id[type]=++cnt;
            t[id[type]].push_back( (node){ x,y } );
            r=max( r,y );
        }
        while( l<=r )
        {
            int mid=(l+r)/2;
            if( check( mid ) )   l=mid+1;
            else   r=mid-1;
        }
        printf("%lld\n",l-1);
        id.clear();
        for(int i=1;i<=cnt;i++)   t[i].clear();
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值