栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

poj 3497 Assemble

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

poj 3497 Assemble

#include <iostream>#include <cstdlib>#include <cstring>#include <cstdio>#include <algorithm>using namespace std;#define maxn 1005#define maxl 25struct Elem{    int id, quality, price;}elem[maxn];int n, budget;char name[maxn][maxl];int tot;bool vis[maxn];int f[maxn];bool operator < (const Elem &a, const Elem &b){    return a.price < b.price;}int getid(char *st){    for (int i = 0; i < tot; i++)        if (strcmp(st, name[i]) == 0) return i;    strcpy(name[tot], st);    return tot++;}void input(){    char st[maxl];    scanf("%d%d", &n, &budget);    tot = 0;    for (int i = 0; i < n; i++)    {        scanf("%s", st);        elem[i].id = getid(st);        scanf("%s%d%d", st, &elem[i].price, &elem[i].quality);    }}bool ok(int a){    int cnt = 0;    long long temp = 0;    memset(vis, 0, sizeof(vis));    for (int i = 0; i < n; i++)        if (!vis[elem[i].id] && elem[i].quality >= a)        { vis[elem[i].id] = true; cnt++; temp += elem[i].price;        }    return temp <= budget;}int binarysearch(){    int l = 0;    int r = 1000000000;    memset(f, 0, sizeof(f));    for (int i = 0; i < n; i++)        f[elem[i].id] = max(f[elem[i].id], elem[i].quality);    for (int i = 0; i < tot; i++)        r = min(r, f[i]);    while (l < r)    {        int mid = (l + r + 1) / 2;        if (!ok(mid)) r = mid - 1;        else l = mid;    }    return l;}int main(){    int t;    scanf("%d", &t);    while (t--)    {        input();        sort(elem, elem + n);        int ans = binarysearch();        printf("%dn", ans);    }    return 0;}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/373858.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号