Alice's Print Service


Time Limit: 2 Seconds      Memory Limit: 65536 KB

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 题意: 打印k页的资料,给出n中付费方案,一次打印超过s1但不超过s2的每页收费p1,超过s2不超过s3的收费p2.....数据保证0=s1<s2<...<sn,p1>=p1>=p3>=...>=pn。
接下来m个查询,对于每个查询问最少花多少钱?例如s1=0 s2=100  p1=20 p2=10 的时候,若要打印99页,显然直接打印100页要更便宜一点..所以结果是1000.. AC代码:
 #include <iostream>
#include <sstream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <vector>
#include <set>
#include <cctype>
#include <algorithm>
#include <cmath>
#include <deque>
#include <queue>
#include <map>
#include <stack>
#include <list>
#include <iomanip> using namespace std; #define INF 0x7fffffff
#define maxn 100010
typedef unsigned long long ll; int n, m;
struct price
{
int s, p;
ll sum;
}a[maxn];
ll _min[maxn];
int BinarySearch(int x)
{
int l = , r = n;
int mid = l + (r-l)/;
while(l < r)
{
if(a[mid].s < x) {
if(a[mid+].s > x) return mid;
l = mid;
}
else if(a[mid].s > x) {
if(a[mid-].s <= x) return mid-;
r = mid;
}
else return mid;
mid = l + (r-l)/;
}
return l;
} int main()
{
int T;
scanf("%d", &T);
while(T--)
{ scanf("%d%d", &n, &m);
for(int i = ; i < n; i++){
scanf("%d%d", &a[i].s, &a[i].p);
a[i].sum = (ll)a[i].s * a[i].p;
}
_min[n-] = a[n-].sum;//注意此处的处理
for(int i = n-; i >= ; i--)
{
_min[i] = min(_min[i+], a[i].sum);
} while(m--)
{
int num;
ll ans = ;
scanf("%d", &num);
if(a[n-].s <= num)
printf("%lld\n", (ll)num*a[n-].p);
else
{
int pos = BinarySearch(num);
ans = (ll)num*a[pos].p;
ans = min(_min[pos+], ans);
printf("%lld\n", ans);
}
}
}
return ;
}

注意事项:

1、_min数组的存在:

  很多人遗忘了这种情况:0     40;

             10   30;

             20   20;

             40   4;

  若要打印15份,最少情况其实可以使打印40份(可打空白页),花费160元。这就是考虑_min数组的必要性;

2、二分法查找pos:注意因为题意要求,所以会有些小不同;

2013 ACM/ICPC 长沙现场赛 A题 - Alice's Print Service (ZOJ 3726)的更多相关文章

  1. 2013 ACM/ICPC 长沙现场赛 C题 - Collision (ZOJ 3728)

    Collision Time Limit: 2 Seconds      Memory Limit: 65536 KB      Special Judge There's a round medal ...

  2. 2013 ACM/ICPC 长沙网络赛J题

    题意:一个数列,给出这个数列中的某些位置的数,给出所有相邻的三个数字的和,数列头和尾处给出相邻两个数字的和.有若干次询问,每次问某一位置的数字的最大值. 分析:设数列为a1-an.首先通过相邻三个数字 ...

  3. hdu 4435 第37届ACM/ICPC天津现场赛E题

    转载请注明出处,谢谢http://blog.csdn.net/ACM_cxlove?viewmode=contents    by---cxlove 题目:给出N个城市,从1开始需要遍历所有点,选择一 ...

  4. 2013 ACM/ICPC 长春网络赛E题

    题意:给出一个字符串,要从头.尾和中间找出三个完全相等的子串,这些串覆盖的区间互相不能有重叠部分.头.尾的串即为整个字符串的前缀和后缀.问这个相同的子串的最大长度是多少. 分析:利用KMP算法中的ne ...

  5. 2013 ACM/ICPC 长春网络赛F题

    题意:两个人轮流说数字,第一个人可以说区间[1~k]中的一个,之后每次每人都可以说一个比前一个人所说数字大一点的数字,相邻两次数字只差在区间[1~k].谁先>=N,谁输.问最后是第一个人赢还是第 ...

  6. 2013 ACM/ICPC 南京网络赛F题

    题意:给出一个4×4的点阵,连接相邻点可以构成一个九宫格,每个小格边长为1.从没有边的点阵开始,两人轮流向点阵中加边,如果加入的边构成了新的边长为1的小正方形,则加边的人得分.构成几个得几分,最终完成 ...

  7. 2013 ACM/ICPC 杭州网络赛C题

    题意:驴和老虎,在一个矩阵的两个格子里,有各自的起始方向.两者以相同的速度向前移动,前方不能走时驴总是向右,老虎总是向左.他们不能超出矩阵边界也不能走自己走过的格子(但可以走对方走过的格子).如果不能 ...

  8. hdu 4432 第37届ACM/ICPC天津现场赛B题

    题目大意就是找出n的约数,然后把约数在m进制下展开,各个数位的每一位平方求和,然后按m进制输出. 模拟即可 #include<cstdio> #include<iostream> ...

  9. HDU 4800/zoj 3735 Josephina and RPG 2013 长沙现场赛J题

    第一年参加现场赛,比赛的时候就A了这一道,基本全场都A的签到题竟然A不出来,结果题目重现的时候1A,好受打击 ORZ..... 题目链接:http://acm.hdu.edu.cn/showprobl ...

随机推荐

  1. #ifdef _cplusplus (转)

    原文不可考,转载链接:http://blog.csdn.net/owldestiny/article/details/5772916 有发现原文的请告知,我会及时更新. 时常在cpp的代码之中看到这样 ...

  2. 面向对象基础(class0425)字符串与集合

    常用类库 学习.net就是学习它的无数个类库怎么用,先看两个简单的 String 字符串,不可变特性.字符串可以看成字符数组 属性 Length 方法 IsNullOrEmpty() 静态方法 ToC ...

  3. 广州大学华软软件学院——NA视频下载

    准备工具: 360极速浏览器(不要认错图标了): 浏览器视频下载插件: 第一步:安装浏览器插件 1.打开浏览器 2.解压,找到插件文件: 3.把插件拖到浏览器中: 4.添加,然后就完成了插件安装 查看 ...

  4. 【openstack报错】【metadata问题】‘http://169.254.169.254/2009-04-04/meta-data/instance-id’ failed : url error [[Errno 111] Connection refused]

    [时间]2014年2月25日 [平台]ubuntu 12.04.3 openstack havana  with nova-network in multi-host [日志]实例启动时输出的日志内容 ...

  5. HW6.14

    import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner i ...

  6. [转]常用的130个vim命令

    原帖地址:http://www.oschina.net/news/43167/130-essential-vim-commands 从 1970 年开始,vi 和 vim 就成为了程序员最喜爱的文本编 ...

  7. Poj2002 Squares

    题意描述:有一堆平面散点集,任取四个点,求能组成正方形的不同组合方式有多少.相同的四个点,不同顺序构成的正方形视为同一正方形. 思路变迁: 1.最简单的方法,直接暴力搜索,即依次取四个顶点,根据其坐标 ...

  8. hdoj 2032 杨辉三角

    杨辉三角 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

  9. 备份数据表为insert 脚本

    unit Unit1; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System ...

  10. C#实现异步编程的两个简单机制(异步委托&定时器)及Thread实现多线程

    创建线程的常用方法:异步委托.定时器.Thread类 理解程序.进程.线程三者之间的区别:简而言之,一个程序至少有一个进程,一个进程至少有一个线程进程就是在内存中运行的程序(即运行着的程序):一个进程 ...