题目

https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1972

题意

给定预算,要求各种类配件一个,品质最小最大问题

思路

如刘书思路。

有一个O(n)时间判定答案是否正确的确定性or随机判断器 + 判断算法是现代算法,随机算法,量子加密的基础,必须要掌握的思路。

感想

1. Uva 很快过了,但是UvaLA怎么也不过,后来发现是 if(mp.count(key)==0)mp[key] = mp.size();这种姿势在g++中行不通。虽然不明白是为什么,但是g++总会有各种小bug。

代码

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <tuple>
#include <set>
#include <map>
#include <cassert>
#define LOCAL_DEBUG
using namespace std;
typedef pair<int, int> MyPair;
const int MAXN = 1e3 + ;
MyPair items[MAXN][MAXN];
int itemsCnt[MAXN];
int leastPrices[MAXN][MAXN];
int budget;
int kindsNum; bool check(int quaMn) {
int remain = budget;
for (int kind_ind = ; kind_ind < kindsNum; kind_ind++) {
int cnt = itemsCnt[kind_ind];
if (quaMn > items[kind_ind][cnt - ].first)return false;
int ind = lower_bound(items[kind_ind], items[kind_ind] + cnt, MyPair(quaMn, )) - items[kind_ind];
remain -= leastPrices[kind_ind][ind];
if (remain < )return false;
}
return true;
} int search(int l, int r) {
int mid = (l + r) >> ;
if (mid == l)return l;
if (check(mid)) {
return search(mid, r);
}
else {
return search(l, mid);
}
} int main() {
#ifdef LOCAL_DEBUG
freopen("input.txt", "r", stdin);
//freopen("output2.txt", "w", stdout);
#endif // LOCAL_DEBUG
int T;
scanf("%d", &T);
for (int ti = ; ti <= T; ti++) {
memset(itemsCnt, , sizeof(itemsCnt));
map<string, int> kinds_map;
int n;
scanf("%d%d", &n, &budget);
kindsNum = ;
int min_qua = 1e9 + , max_qua = ;
for (int i = ; i < n; i++) {
char tmp[];
scanf("%s", tmp);
if (kinds_map.count(tmp) == ) {
kinds_map[tmp] = kindsNum++;
}
int kind_ind = kinds_map[tmp];
scanf("%s", tmp);
int price, qua;
scanf("%d%d", &price, &qua);
items[kind_ind][itemsCnt[kind_ind]++] = MyPair(qua, price);
min_qua = min(min_qua, qua);
max_qua = max(max_qua, qua);
}
for (int kind_ind = ; kind_ind < kindsNum; kind_ind++) {
int cnt = itemsCnt[kind_ind];
sort(items[kind_ind], items[kind_ind] + cnt);
for (int j = ; j < cnt; j++) {
leastPrices[kind_ind][j] = items[kind_ind][j].second;
}
for (int j = ; j < cnt; j++) {
if (items[kind_ind][j].first == items[kind_ind][j - ].second) {
leastPrices[kind_ind][j] = leastPrices[kind_ind][j - ];
}
}
for (int j = cnt - ; j >= ; j--) {
leastPrices[kind_ind][j] = min(leastPrices[kind_ind][j + ], leastPrices[kind_ind][j]);
}
}
int ans = search(, max_qua + );
printf("%d\n", ans);
} return ;
}

Uva 12124 Uva Live 3971 - Assemble 二分, 判断器, g++不用map.size() 难度:0的更多相关文章

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

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

  2. UVa 10905 - Children's Game 排序,题目没有说输入是int 难度: 0

    题目 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&a ...

  3. UVa 10341 - Solve It【经典二分,单调性求解】

    原题: Solve the equation:         p*e-x + q*sin(x) + r*cos(x) + s*tan(x) + t*x2 + u = 0         where  ...

  4. Uva LA 3177 - Beijing Guards 贪心,特例分析,判断器+二分,记录区间内状态数目来染色 难度: 3

    题目 https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_pr ...

  5. 训练指南 UVA - 11090(最短路BellmanFord+ 二分判负环)

    layout: post title: 训练指南 UVA - 11090(最短路BellmanFord+ 二分判负环) author: "luowentaoaa" catalog: ...

  6. 训练指南 UVA - 11478(最短路BellmanFord+ 二分+ 差分约束)

    layout: post title: 训练指南 UVA - 11478(最短路BellmanFord+ 二分+ 差分约束) author: "luowentaoaa" catal ...

  7. uvalive 3971 - Assemble(二分搜索 + 贪心)

    题目连接:3971 - Assemble 题目大意:有若干个零件, 每个零件给出的信息有种类, 名称, 价格, 质量,  现在给出一个金额, 要求在这个金额范围内, 将每个种类零件都买一个, 并且尽量 ...

  8. POJ_2318_TOYS&&POJ_2398_Toy Storage_二分+判断直线和点的位置关系

    POJ_2318_TOYS&&POJ_2398_Toy Storage_二分+判断直线和点的位置 Description Calculate the number of toys th ...

  9. 软件测试技术(四)——闰年判断器+ int.Parse错误如何解决

    目标程序 本次所测试的目标程序是一个闰年判断器,我们知道,一般情况下年份被4整除就可以了,但是如果遇到百年的时候还需要被400整除,于是有了如下的逻辑判断: bool isRunNian = fals ...

随机推荐

  1. win10新系统修改onedrive目录,提示找不到OneDrive目录

    win10不知更新了什么,x1c非常卡一跳一跳的,很多年没见过了-_-!!( 原因排查:http://www.cnblogs.com/xuanmanstein/p/8878180.html). 于是重 ...

  2. POI 导入导出时异常[java.io.IOException: Broken pipe]

    使用用POI导出文件时抛出异常java.io.IOException: Broken pipe ERROR: 'java.io.IOException: Broken pipe' org.apache ...

  3. Asp.net core 学习笔记 ( ef core )

    更新 : 2018-11-26 这里记入一下关于 foreignKey cascade action 默认情况下如果我们使用 data annotation required + foreginkey ...

  4. python中configparser模块的使用

    configparser模块用于生成和修改常见配置文档,当前模块的名称在 python 3.x 版本中变更为 configparser. 首先要写一个如下所示的配置文件: [DEFAULT] serv ...

  5. (转+整理)C#中使用GUID

    GUID(全局统一标识符)是指在一台机器上生成的数字,它保证对在同一时空中的所有机器都是唯一的.通常平台会提供生成GUID的API.生成算法很有意思,用到了以太网卡地址.纳秒级时间.芯片ID码和许多可 ...

  6. (9)进程---JoinableQueue队列

    消费者模型-->存和取得过程 和Queue队列区别:解决了Queue队列拿取完,程序阻塞不能自动关闭(依靠放入None来解决)的问题--->参见上个例子 put 存入, get 获取 q. ...

  7. lncRNA表达定量方法评估

    见文章:Benchmark of lncRNA Quantification for RNA-Seq of Cancer Samples Overall, 10-16% of lncRNAs can ...

  8. hdu-3001 三进制状态压缩+dp

    用dp来求最短路,虽然效率低,但是状态的概念方便解决最短路问题中的很多限制,也便于压缩以保存更多信息. 本题要求访问全图,且每个节点不能访问两次以上.所以用一个三进制数保存全图的访问状态(3^10,空 ...

  9. Confluence 6 指派空间权限概念

    如果你是一个空间的管理员,你可以对空间的使用权限进行控制.你可以为独立用户或者Confluence Groups的权限进行权限的指派/收回. Confluence 管理员可以将用户分配到用户组中,这样 ...

  10. 55-56 ORM多表查询

    多表查询: KEY   ====>  通过ORM引擎如何跨表: 正向查询按字段,反向查询按表名小写 模型的创建: from django.db import models # Create yo ...