题目连接:3971 - Assemble

题目大意:有若干个零件, 每个零件给出的信息有种类, 名称, 价格, 质量,  现在给出一个金额, 要求在这个金额范围内, 将每个种类零件都买一个, 并且尽量让这些零件中质量最小的越大, 输出质量最小的值。

解题思路:首先可以用二分搜索确定质量, 然后在搜索的过程中要判断这个质量是否能被满足, 判断函数可以用贪心, 在每一类的零件中选择价格最低且质量大于等于当前质量的零件。(事先按照价格大小排序)。

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
const int N = 1005;
const int M = 25; struct State {
char name[M];
int money;
int wei;
}s[N][N];
int cnt, sum, son[N];
char str[N][M], sex[M]; bool cmp(const State& a, const State& b) {
return a.money < b.money;
} int find(char sex[]) {
for (int i = 0; i < cnt; i++)
if(strcmp(sex, str[i]) == 0)
return i;
strcpy(str[cnt], sex);
return cnt;
} bool judge(int Min) {
int tmp = 0;
for (int i = 0; i < cnt; i++) {
int flag = 1;
for (int j = 0; j < son[i]; j++) {
if (s[i][j].wei >= Min) {
flag = 0;
tmp += s[i][j].money;
break;
}
}
if (flag || tmp > sum) return false;
}
return true;
} int main() {
State now;
int cas, n, cur, L, R;
scanf("%d", &cas);
while (cas--) {
memset(s, 0, sizeof(s));
memset(son, 0, sizeof(son));
memset(str, 0, sizeof(str));
cnt = L = R = 0; scanf("%d%d", &n, &sum);
for (int i = 0; i < n; i++) {
scanf("%s%s%d%d", sex, now.name, &now.money, &now.wei);
if (now.wei > R) R = now.wei;
cur = find(sex);
s[cur][son[cur]++] = now;
if (cur == cnt) cnt++;
} for (int i = 0; i < cnt; i++)
sort(s[i], s[i] + son[i], cmp); int mid;
while (L < R) {
mid = (L + R) / 2;
if (L == mid) break;
if (judge(mid))
L = mid;
else
R = mid;
}
if (judge(mid + 1)) mid++;
printf("%d\n", mid);
}
return 0;
}

uvalive 3971 - Assemble(二分搜索 + 贪心)的更多相关文章

  1. UVA 12124 UVAlive 3971 Assemble(二分 + 贪心)

    先从中找出性能最好的那个数, 在用钱比較少的去组合,能组出来就表明答案在mid的右边,反之在左边, #include<string.h> #include<map> #incl ...

  2. UVALive 3971 Assemble(模拟 + 二分)

    UVALive 3971 题意:有b块钱.想要组装一台电脑,给出n个配件的种类,名字,价格,品质因子.若各种类配件各买一个,总价格<=b,求最差品质配件的最大品质因子. 思路: 求最大的最小值一 ...

  3. UVALive 3971 Assemble(二分+贪心)

    本题思路不难,但是要快速准确的AC有点儿考验代码功力. 看了大白书上的标程,大有所获. 用map和vector的结合给输入分组,这个数据结构的使用非常精美,恰到好处. #include<iost ...

  4. UVaLive 3971 Assemble (水题二分+贪心)

    题意:你有b元钱,有n个配件,每个配件有各类,品质因子,价格,要每种买一个,让最差的品质因子尽量大. 析:很简单的一个二分题,二分品质因子即可,每次计算要花的钱的多少,每次尽量买便宜且大的品质因子. ...

  5. uvalive 3971 Assemble

    https://vjudge.net/problem/UVALive-3971 题意: 现在你要组装一台电脑,每个电脑的一种类型的配件都有多种选择,它们的名字是不同的. 现在给出已有的元件,每种类型都 ...

  6. nyoj914Yougth的最大化(二分搜索 + 贪心)

    Yougth的最大化 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描述 Yougth现在有n个物品的重量和价值分别是Wi和Vi,你能帮他从中选出k个物品使得单位重量的价值最大吗 ...

  7. nyoj914(二分搜索+贪心)

    题目意思: pid=914">acm.nyist.net/JudgeOnline/problem.php?pid=914 如今有n个物品的重量和价值各自是Wi和Vi,你能帮他从中选出k ...

  8. UVALive 4225 Prime Bases 贪心

    Prime Bases 题目连接: https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&a ...

  9. UVALive 7464 Robots (贪心)

    Robots 题目链接: http://acm.hust.edu.cn/vjudge/contest/127401#problem/K Description http://7xjob4.com1.z ...

随机推荐

  1. 文件上传功能 -- jquery.form.js/springmvc

    距离上一篇 文件上传下载样式 -- bootstrap(http://www.cnblogs.com/thomascui/p/5370947.html)已经三周时间了,期间一直考虑怎么样给大家提交一篇 ...

  2. linux定时任务crontab的设置

    linux定时任务crontab的设置http://www.blogjava.net/freeman1984/archive/2010/09/23/332715.html vi /etc/cronta ...

  3. JavaSE学习总结第26天_网络编程

      26.01  网络编程概述 网络编程:就是用来实现网络互连的不同计算机上运行的程序间可以进行数据交换. 26.02  网络模型概述和图解 计算机网络之间以何种规则进行通信,就是网络模型研究问题. ...

  4. BZOJ 2442: [Usaco2011 Open]修剪草坪( dp )

    dp dp[ i ] 表示第 i 个不选 , 前 i 个的选择合法的最小损失 , dp[ i ] = min( dp[ j ] ) ( max( 0 , i - 1 - k ) <= j < ...

  5. struts的由来

    当学习或工作时,有些同学会谈到熟悉struts.hibernate.spring等等框架,貌似熟悉这些框架是精通java的表现,但是我们应该首先弄明白为什么要学框架?是为了学习而学习?还是为了工作而学 ...

  6. Dojo实现Tabs页报错(一)

    1.在用Dojo写tab页的过程中出现了一些错误 dojo源码如下: <%-- Document : grid Created on : 2013-12-15, 18:05:47 Author ...

  7. Linux 网络编程: daytime Service

    前言 如果你这段时间过得很舒服,那就证明你荒废以一段时间.如果你这段时间过得很辛苦,那么恭喜,你又进步了.最近入党的事情忙得焦头烂额,博客也拖了好久没写,主要也是因为要装 xinetd 服务一直没装好 ...

  8. [LeetCode]题解(python):099-Recover Binary Search Tree

    题目来源: https://leetcode.com/problems/recover-binary-search-tree/ 题意分析: 二叉搜索树中有两个点错了位置,恢复这棵树. 题目思路: 如果 ...

  9. github桌面软件使用教程

    github桌面软件使用教程 首先 要先安装 桌面版官网,或者百度搜github windows下载即可 可以再github网站上直接点击,把代码添加的桌面软件中 也可以再左上角添加项目,比如actu ...

  10. PowerShell Remove all user defined variable in PowerShell

    When PS scripts executes, it is possibly create much user defined variables. So, sometimes these var ...