题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4791

题面:

Alice's Print Service

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1596    Accepted Submission(s): 380

Problem Description
Alice is providing print service, while the pricing doesn't seem to be reasonable, so people using her print service found some tricks to save money.
For example, the price when printing less than 100 pages is 20 cents per page, but when printing not less than 100 pages, you just need to pay only 10 cents per page. It's easy to figure out that if you want to print 99 pages, the best choice is to print an extra blank page so that the money you need to pay is 100 × 10 cents instead of 99 × 20 cents.
Now given the description of pricing strategy and some queries, your task is to figure out the best ways to complete those queries in order to save money.
Input
The first line contains an integer T (≈ 10) which is the number of test cases. Then T cases follow.
Each case contains 3 lines. The first line contains two integers n, m (0 < n, m ≤ 105 ). The second line contains 2n integers s1, p1 , s2, p2 , ..., sn, pn (0=s1 < s2 < ... < sn ≤ 109 , 109 ≥ p1 ≥ p2 ≥ ... ≥ pn ≥ 0).. The price when printing no less than si but less than si+1 pages is pi cents per page (for i=1..n-1). The price when printing no less than sn pages is pn cents per page. The third line containing m integers q1 .. qm (0 ≤ qi ≤ 109 ) are the queries.
Output
For each query qi, you should output the minimum amount of money (in cents) to pay if you want to print qi pages, one output in one line.
Sample Input
1 2 3 0 20 100 10 0 99 100
Sample Output
0 1000 1000
Source
2013 Asia Changsha Regional Contest

题意:

打印张数越多价格区间越优惠,(也可能相同)。问给定张数为n的时候,打印多少张最省钱,即虽然打印多了,但是单价低了,故整个价格也就自然低了。问最后花多少钱。

解题:

先预处理出每个区间的最小花费,即它的临界值乘以单价,然后从小到大排个序。对于每组样例,算出它在本身区间内的花费,然后用二分法(直接用了upper_bound函数)找出小于其花费的最后一个位置P。从0开始遍历最小花费数组,直至P。若遍历过程中发现,有合法区间,即该区间对应张数大于本身张数,则停止遍历,输出当前代价。其实这样写,算是水过的吧(700ms),复杂度比较高了。

比较好的解法是先预处理出每个区间的最优值,然后每输入一个数,只要找到该数属于哪个区间即可。(minn的意义是指该区间范围内打印临界张数的最优值)预处理过程为 minn[i]=min(minn[i],minn[i+1]); minn[i]的初始值为直接在该区间打印临界张数的代价。i从大到小遍历一遍即可。因为i+1的张数大于i的张数,故minn[i]可以直接取,minn[i+1]的值,又因为minn[i]是从大到小求得,minn[i+1]保存的是即是后面所有的最优值。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <map>
#include <cmath>
#include <cstdlib>
#include <set>
#include <algorithm>
#include <string>
#include <iomanip>
#define LL long long
using namespace std;
struct cost
{int num;long long minn;
}store[100010];
bool cmp(cost a,cost b)
{return a.minn<b.minn;
}
LL mini[100010];
int main()
{int storep[100010],stores[100010];int t,n,m,p,tmp;bool flag;LL val;scanf("%d",&t);while(t--){scanf("%d%d",&n,&m);for(int i=0;i<n;i++){scanf("%d%d",&stores[i],&storep[i]);store[i].num=i;store[i].minn=1LL*storep[i]*stores[i];}sort(store,store+n,cmp);for(int i=0;i<n;i++){mini[i]=store[i].minn;}for(int i=0;i<m;i++){scanf("%d",&tmp);if(tmp>=stores[n-1]){printf("%lld\n",1LL*tmp*storep[n-1]);}else{flag=false;p=lower_bound(stores,stores+n,tmp)-stores;//cout<<"storep: "<<storep[p]<<endl;val=1LL*storep[p-1]*tmp;// for(int i=0;i<p+1;i++)//cout<<"price: "<<storep[i]<<endl;//cout<<"val: "<<val<<endl;p=upper_bound(mini,mini+n,val)-mini;for(int i=0;i<p;i++){if(stores[store[i].num]>=tmp){flag=true;printf("%lld\n",store[i].minn);break;}}if(!flag){if(stores[store[p].num]>=tmp&&mini[p]<=val){flag=true;printf("%lld",store[p].minn);}}if(!flag){printf("%lld\n",val);}}}}return 0;
}

HDU 4791 Alice's Print Service相关推荐

  1. NYOJ 933 Bob's Print Service

    Bob's Print Service 时间限制:1000 ms  |  内存限制:65535 KB 难度:3 描述 Bob is providing print service,while the ...

  2. android三星打印插件,三星打印服务插件 Samsung Print Service for Android

    三星手机用户想要使用打印机服务,必须要有插件才行,三星打印服务插件 Samsung Print Service for Android该插件使手机文档打印到许多三星激光打印机,且无需对第三方应用程序或 ...

  3. Mopria Print Service现在可用于统一端点管理解决方案

    Mopria Print Service已登录移动企业集成解决方案平台MobileIron MarketPlace 加州圣拉蒙--(美国商业资讯)--为打印和扫描提供通用标准和解决方案的全球非营利性会 ...

  4. android print service,Mopria Print Service

    Mopria Print Service 介绍 Mopria Print Service Mopria Print Service enables printing on your Android ( ...

  5. hdu 5708 Alice and Bob(尼姆博弈)

    题目链接:hdu 5708 Alice and Bob Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65535/65536 K (Java ...

  6. hdu 4268 Alice and Bob(STL版)

    http://acm.hdu.edu.cn/showproblem.php?pid=4268 这是今天网络赛的水题,下午短路了,没想到怎么做.队友hq是用treap做的,不过赛后我才想懂怎么做,回到宿 ...

  7. HDU 4111 Alice and Bob 【DP解决博弈】

    http://acm.hdu.edu.cn/showproblem.php?pid=4111 题目大意:详见http://acm.hrbust.edu.cn/index.php?m=ProblemSe ...

  8. HDU 4122 Alice's mooncake shop 单调队列优化dp

    Alice's mooncake shop Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem ...

  9. HDU 6249 Alice’s Stamps(dp)

    http://acm.hdu.edu.cn/showproblem.php?pid=6249 题意: 给出n个区间,求选k个区间的最大区间并. 思路: 可能存在左端点相同的多个区间,那么此时我们肯定选 ...

  10. HDU 4371 Alice and Bob

    比赛是木做出来,回头看看,是当时考虑的太复杂了,si = si-1 + dk or si = si-1 - dk , and si-2 < si<= n, 1 <= k <= ...

最新文章

  1. Android webview 加载的html 无法显示弹框
  2. 中文速读微生物组(宏基因组)前沿文献——这个可以有
  3. 亲历者说 | 完整记录一年多考拉海购的云原生之路
  4. 系统美化 XP主题及其他
  5. lombok之@Slf4j注解
  6. msp单片机UCS配置
  7. 最大隶属度原则_【模糊数学课程笔记】六、模糊模型识别I(最大隶属度原则)...
  8. s3c6410烧写u-bootLinux
  9. ADT-bundle(Android Development Tools)环境配置
  10. 如何选购晨检机器人_如何选购扫地机器人?吸力/清扫导航/路线规划缺一不可...
  11. 高速串行收发器的预加重与均衡
  12. JavaWeb调用顺序
  13. View和ViewGroup中的mParent
  14. Ionic5项目android打包流程
  15. 共享文件问题: 你可能没有权限使用网络资源。请与这台服务器的管理员联系以查明你是否有访问权限。
  16. Visual Leak Detector使用方法
  17. 自动生成_一键自动生成CAD图纸目录
  18. 一步一步开发Game服务器(二)登陆2
  19. 连接池的原理以及分析
  20. 新书推荐 |《用户增长方法论:找到产品长盛不衰的增长曲线》

热门文章

  1. 印度官方语言有几种_印度货币上有17种语言,你知道每种语言有多少人在用吗?...
  2. Boost.Asio Library
  3. BZOJ-3231 递归数列 矩阵连乘+快速幂
  4. 树莓派linux led字符设备驱动( linux自带)
  5. ubuntu 20安装NVIDIA驱动并处理蓝色背景的界面 perform mok management
  6. (数据结构)二叉树中序遍历
  7. 软件公司是如何招聘人才的?
  8. 分享:一个轻量级的企业Wiki和团队知识分享平台CMS
  9. xlsx格式表格汉字批量转音节,不带声调(python)
  10. 阳线双响炮K线形态,上升势不可挡,送指标源码