Uva 12124 Uva Live 3971 - Assemble 二分, 判断器, g++不用map.size() 难度:0
题目
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的更多相关文章
- UVA 12124 UVAlive 3971 Assemble(二分 + 贪心)
先从中找出性能最好的那个数, 在用钱比較少的去组合,能组出来就表明答案在mid的右边,反之在左边, #include<string.h> #include<map> #incl ...
- UVa 10905 - Children's Game 排序,题目没有说输入是int 难度: 0
题目 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&a ...
- UVa 10341 - Solve It【经典二分,单调性求解】
原题: Solve the equation: p*e-x + q*sin(x) + r*cos(x) + s*tan(x) + t*x2 + u = 0 where ...
- Uva LA 3177 - Beijing Guards 贪心,特例分析,判断器+二分,记录区间内状态数目来染色 难度: 3
题目 https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_pr ...
- 训练指南 UVA - 11090(最短路BellmanFord+ 二分判负环)
layout: post title: 训练指南 UVA - 11090(最短路BellmanFord+ 二分判负环) author: "luowentaoaa" catalog: ...
- 训练指南 UVA - 11478(最短路BellmanFord+ 二分+ 差分约束)
layout: post title: 训练指南 UVA - 11478(最短路BellmanFord+ 二分+ 差分约束) author: "luowentaoaa" catal ...
- uvalive 3971 - Assemble(二分搜索 + 贪心)
题目连接:3971 - Assemble 题目大意:有若干个零件, 每个零件给出的信息有种类, 名称, 价格, 质量, 现在给出一个金额, 要求在这个金额范围内, 将每个种类零件都买一个, 并且尽量 ...
- POJ_2318_TOYS&&POJ_2398_Toy Storage_二分+判断直线和点的位置关系
POJ_2318_TOYS&&POJ_2398_Toy Storage_二分+判断直线和点的位置 Description Calculate the number of toys th ...
- 软件测试技术(四)——闰年判断器+ int.Parse错误如何解决
目标程序 本次所测试的目标程序是一个闰年判断器,我们知道,一般情况下年份被4整除就可以了,但是如果遇到百年的时候还需要被400整除,于是有了如下的逻辑判断: bool isRunNian = fals ...
随机推荐
- leecode第四题(寻找两个有序数组的中位数)
题解: class Solution { public: double findMedianSortedArrays(vector<int>& nums1, vector<i ...
- kafka+docker+python
昨天晚上刚刚才花3小时看完<日志:每个软件工程师都应该知道的有关实时数据的统一概念>. 今天就把kafka在docker容器里运行起来,github上有几个,但都太复杂了. 我自己写个最简 ...
- 04-python-闭包
1.闭包的概念: 官方概念:在一些语言中,在函数中可以(嵌套)定义另一个函数时,如果内部的函数引用了外部的函数的变量,则可能产生闭包.闭包可以用来在一个函数与一组"私有"变量之间创 ...
- sql server auto increment - trace flag 272
从 sql 2012 开始, 微软为了让 insert 时 auto increment 快一些,做了一个 cache 的机制. 这个机制虽然好,但是也有麻烦的情况,如果你的 sql 突然 resta ...
- word2010怎么把白色方框变成黑色方框?
word2010怎么把白色方框变成黑色方框? 打开Word 2010文档,选中第四个白色方框. 切换到“插入”功能区,在符号选项组单击“符号”按钮,出来的窗口单击“其他符号”. 在“符号”选项卡单 ...
- Getting Started with Processing 第五章的easing问题
分析 使用 easing easing 的感官目的是为了 draw 的时候,画的图形不是即时 mouseX 的值,而是稍有落后一点.从算法分析,就是让所画图形的 x 坐标 落后于 mouseX 的值, ...
- ch02 课下作业
2.96遵循位级浮点编码规则,实现具有如下原型的函数: /* *Compute (int) f. *If conversion causes overflow or f is NaN, return ...
- ionic更换加载页面和logo
将你准备要更替的加载页面的图片和logo图片分别重名名为splash.png和icon.png 将这两个图片文件放到项目中的resources目录下,覆盖原有的这两个文件 在cmd或终端进入项目 分别 ...
- codeforces587a//Duff and Weight Lifting// Codeforces Round #326 (Div. 1)
题意:一数列an,如果存在一个k,有2^(ai)+2^(aj)+......=2^k成立,那么一次能拿走ai,aj这些全部.问最少拿的次数. 太简单. 乱码 //#pragma comment(lin ...
- java把list分成几个list
public static void main(String[] args) { List<String> list=new ArrayList<>(); list.add(& ...