HDU4791->贪心&&二分优化
来源:互联网 发布:画设计图的软件 编辑:程序博客网 时间:2024/06/05 14:41
HDU4791->贪心&&二分优化
题意:
有一家打印店,打印超过一定分数后每份的单价就会降低,你需要打印一些文件,你可以打印敲好的份数或者是多打印一些废纸以凑得更低的价格,问打印这些文件所需的最小花费。
题解:
采用贪心策略,从最单价低价开始计算,直到计算到数量的区间正好包含所需印刷的产品数量。
但是单纯采取贪心策略遍历整个价格数组会TLE,所以需要二分优化,先二分找到包含当前价格的价格区间,然后在当前区间内的价格以及数量超过当前区间的区间中选取最低总价。
代码:
#include <stdio.h>#include <iostream>#include <algorithm>using namespace std ;#define MAX 100005#define INF 0x7fffffffffffffffstruct node1{ long long num ,query; long long mini;}ans[MAX];struct node{ long long s , p ; long long sum;}print[MAX];bool comp1(const struct node1&a , const struct node1&b){ return a.query < b.query ;}bool comp2(const struct node1&a , const struct node1&b){ return a.num < b.num ;}int main(){ int T ; int n , m ,q; scanf("%d" , &T) ; while(T --) { scanf("%d%d" , &n , &m) ; for(int i = 0 ; i < n ; i ++) { scanf("%lld%lld" , &print[i].s , &print[i].p) ; print[i].sum = print[i].s*print[i].p ; } for(int i = 0 ; i < m ; i ++) { scanf("%lld" , &ans[i].query) ; ans[i].num = i ; ans[i].mini = INF ; } sort(ans , ans+ m , comp1) ; long long temp = INF ; int cnt = m-1 ; for(int i = n - 1 ; i >= 0 ; i --) { while (cnt>=0&&ans[cnt].query >= print[i].s) { if(ans[cnt].mini > temp) ans[cnt].mini = temp ; if(ans[cnt].query*print[i].p < ans[cnt].mini) { ans[cnt].mini = ans[cnt].query*print[i].p ; } cnt -- ; } if(temp > print[i].sum) { temp = print[i].sum ; } } sort(ans , ans + m,comp2); for(int i = 0 ; i < m ; i ++) { printf("%lld\n" , ans[i].mini) ; } } return 0 ;}
0 0
- HDU4791->贪心&&二分优化
- HDU4791(线段树+二分)
- POJ1505 贪心+二分优化
- bzoj1044 木棍分割 二分+贪心&dp优化
- 二分+贪心
- 贪心 + 二分
- 贪心二分
- 贪心+二分
- 二分图+贪心优化 [2009国家集训队]最大收益
- hdu4791水题
- hdu4791 Labyrinth
- HDU4791【杂】
- 贪心(bnuoj49103+二分+贪心)
- BNU 49103 贪心【二分+贪心】
- hdu pie(二分+贪心)
- POJ3497 Assemble 二分+贪心
- mysterious 二分加贪心
- HDU 3650 贪心+二分
- centos7.0安装php7.0
- 杭电1002。包括了该题一些常见错误
- 后台开发学习——JSP和JavaBean
- uva 144
- 负载均衡技术
- HDU4791->贪心&&二分优化
- 标记一些基础知识
- [HDU 5147] Sequence II (树状数组+前缀和)
- CSS inline-block与vertical-align的一个问题
- 框架
- Python3.4使用MySQL—Python工具连接MySQL
- iOS 自动释放池Autorelease
- listview 中的 item里面带有button按钮的点击实现方法
- 屏幕适配