例题12组装电脑

来源:互联网 发布:台湾高铁网络订票系统 编辑:程序博客网 时间:2024/04/29 14:14

提交传送门:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=13338

主要是map的使用和二分答案。

代码:

#include <cstdio>
#include <iostream>
#include <cstring>
#include <string>
#include <cmath>
#include <queue>
#include <set>
#include <map>
#include <stack>
#include <algorithm>
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define mem(a) memset(a, 0, sizeof(a))
#define eps 1e-5
#define INF 0x3f3f3f3f
using namespace std;
int cnt;
map<string ,int > id;
int ID(string s)
{
    if(!id.count(s))
        id[s]=cnt++;
    return id[s];
}
const int maxn=1000+5;
struct node
{
    int price;
    int quality;
};
int n,b;
vector<node> comp[maxn];
bool ok(int q)
{
    int sum=0;
    for(int i=0;i<cnt;i++)
    {
        int cheapest=b+1;
       int  m=comp[i].size();
        for(int j=0;j<m;j++)
            if(comp[i][j].quality>=q)
                cheapest=min(cheapest,comp[i][j].price);
                if(cheapest==b+1)
                    return false;
                sum+=cheapest;
                if(sum>b)
                    return false;
    }
    return true;
}
int main()
{
    int T;
    cin>>T;
    while(T--)
    {
        cin>>n>>b;
        cnt=0;
        for(int i=0;i<n;i++)
            comp[i].clear();
                   id.clear();
        int maxq=0;
        for(int i=0;i<n;i++)
        {
         char type[30],name[30];
            int x,y;
            cin>>type>>name>>x>>y;
            maxq=max(maxq,y);
            comp[ID(type)].push_back((node){x,y});
        }
        int l=0;
        int r=maxq;
        while(l<r)
        {
            int M=(l+r+1)/2;
            if(ok(M))
                l=M;
            else
                r=M-1;
        }
        cout<<l<<endl;
    }
}

0 0