Try Again

HDU 2333 Assemble(二分)

Assemble

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 783    Accepted Submission(s): 297

Problem 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 <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cstdlib>
#include <iomanip>
#include <cmath>
#include <ctime>
#include <map>
#include <set>
using namespace std;
#define lowbit(x) (x&(-x))
#define max(x,y) (x>y?x:y)
#define min(x,y) (x<y?x:y)
#define MAX 100000000000000000
#define MOD 1000000007
#define pi acos(-1.0)
#define ei exp(1)
#define PI 3.141592653589793238462
#define INF 0x3f3f3f3f3f
#define mem(a) (memset(a,0,sizeof(a)))
typedef long long ll;
const int N=1005;
const int mod=1e9+7;
int value[N],k,n,m,t;
struct Node
{
    char type[30];
    char name[30];
    int cost,val;
    friend bool operator<(const Node&a,const Node&b)
    {
        if(strcmp(a.type,b.type)==0) return a.val==b.val?a.cost<b.cost:a.val>b.val;//质量相等返回价格低的
        return strcmp(a.type,b.type)<0;
    }
}node[N];
vector<Node>v[N];
bool check(int mid)
{
    ll ans=0;
    for(int i=0;i<k;i++)
    {
        int pos=mod;
        for(int j=0;j<v[i].size();j++)
        {
            if(v[i][j].val>=mid) pos=min(pos,v[i][j].cost);//固定质量,每种类型娶价格最小
        }
        ans+=pos;
        if(ans>m)return 0;
    }
    return 1;
}
int main()
{
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&m);
        k=0;
        for(int i=0;i<n;i++)
        {
            v[i].clear();
            scanf("%s %s %d %d",&node[i].type,&node[i].name,&node[i].cost,&node[i].val);
            value[i]=node[i].val;
        }
        sort(value,value+n);
        sort(node,node+n);
        for(int i=0;i+1<n;i++)
        {
            while(i+1<n&&strcmp(node[i].type,node[i+1].type)==0)
            {
                v[k].push_back(node[i]);
                i++;
            }
            v[k].push_back(node[i]);
            k++;
        }
        int l=0,r=n-1;
        int mid=(r+l)>>1;
        while(l<=r)
        {
            if(check(value[mid])) l=mid+1;
            else r=mid-1;
            mid=(l+r)>>1;
        }
        printf("%d\n",value[mid]);
    }
    return 0;
}

 

posted @ 2017-07-27 19:26  十年换你一句好久不见  阅读(283)  评论(0编辑  收藏  举报