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. opencv 在工业中的应用:二维标定

    在工业中经常要检测一个零件的尺寸,但是图像处理得到的是像素值,怎么才能得到实际的毫米值呢?这就要用到二维标定,我用OPENCV写了一个利用标定板进行标定的DEMO. 很多商业软件都没有二维标定的功能, ...

  2. 命令rm

    mv -r 递归删除文件夹内所有东西mv -i 交互式删除mv -f 强制删除,没有警告提示

  3. Excel里的单元格提行

    最近老板发了个表,遇到了个小知识点,收藏以后有用!拿来学学 如下,是属于单元格.怎么提行? 直接,空格或space,都无法解决!...... 解决办法:Alt + Enter 成功! 参考: http ...

  4. Java的位运算符实例——与(&)、非(~)、或(|)、异或(^)

    一.Java的位运算符实例——与(&).非(~).或(|).异或(^) 1.与(&) 0 & 2 = 0 0 0 0 0 1 0 0 1 0 2.非(~) ~0 = 7 0 0 ...

  5. Smack[3]用户列表,头像,组操作,用户操作

    用户列表 Smack主要使用Roster进行列表管理的 connection.getRoster(); /** * 返回所有组信息 <RosterGroup> * * @return Li ...

  6. 通过存储过程进行分页查询的SQL示例

    --创建人:zengfanlong --创建时间:-- :: --说明:根据公司简写代码获取当前待同步的气瓶档案数据(分页获取) ALTER PROCEDURE [UP_GasBottles_GetS ...

  7. 怎样通过iPhone Safari 来安装测试版ipa

    http://www.cocoachina.com/bbs/read.php?tid=94101# <?xml version="1.0" encoding="UT ...

  8. iOS- iPad UIPopoverController

    在IPAD开发中,有一个很有趣的视图控制器,UIPopoverControllr,它的初始化必须要设置一个"内容视图",相当于它本身只是作为一个“容器”,而显示的内容还需要另外一个 ...

  9. thinPHP中多维数组的遍历

    $drug=array(    'ACEI'=>array(array('ch_name'=>'卡托普利','en_name'=>'captopril'),array('ch_nam ...

  10. paip.mysql备份慢的解决

    paip.mysql备份慢的解决.txt 作者Attilax ,  EMAIL:1466519819@qq.com  来源:attilax的专栏 地址:http://blog.csdn.net/att ...