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
#include <iostream>
#include <string>
#include <string.h>
#include <map>
#include <stdio.h>
#include <algorithm>
#include <vector>
#define Min(a,b) ((a)<(b)?(a):(b))
using namespace std ;
const int Max_N = ;
typedef long long LL ;
LL dp[Max_N] ;
LL S[Max_N] ,P[Max_N] ;
int N ,M ; int find_id(LL x){
int Left = ;
int Right = N ;
int mid ;
int ans = -;
while(Left<=Right){
mid = (Left + Right)>> ;
if(S[mid] >= x){
Right = mid - ;
ans = mid ;
}
else
Left = mid + ;
}
return ans ;
} int main(){
int T ;
LL x;
scanf("%d",&T) ;
while(T--){
scanf("%d%d",&N,&M) ;
for(int i = ;i <= N ;i++)
scanf("%lld%lld",&S[i],&P[i]) ;
dp[N] = S[N] * P[N] ;
for(int i = N- ;i >= ;i--){
dp[i] = Min(S[i]*P[i],dp[i+]) ;
}
while(M--){
scanf("%lld",&x) ;
int id = find_id(x) ;
// cout<<x<<" "<<id<<endl ;
if(id == -){
printf("%lld\n",P[N]*x) ;
continue ;
}
if(S[id] == x)
printf("%lld\n",dp[id]) ;
else
printf("%lld\n",Min(dp[id],x*P[id-])) ;
}
}
return ;
}

The 2013 ACM-ICPC Asia Changsha Regional Contest - A的更多相关文章

  1. hduoj 4710 Balls Rearrangement 2013 ACM/ICPC Asia Regional Online —— Warmup

    http://acm.hdu.edu.cn/showproblem.php?pid=4710 Balls Rearrangement Time Limit: 6000/3000 MS (Java/Ot ...

  2. hduoj 4708 Rotation Lock Puzzle 2013 ACM/ICPC Asia Regional Online —— Warmup

    http://acm.hdu.edu.cn/showproblem.php?pid=4708 Rotation Lock Puzzle Time Limit: 2000/1000 MS (Java/O ...

  3. hduoj 4715 Difference Between Primes 2013 ACM/ICPC Asia Regional Online —— Warmup

    http://acm.hdu.edu.cn/showproblem.php?pid=4715 Difference Between Primes Time Limit: 2000/1000 MS (J ...

  4. hduoj 4712 Hamming Distance 2013 ACM/ICPC Asia Regional Online —— Warmup

    http://acm.hdu.edu.cn/showproblem.php?pid=4712 Hamming Distance Time Limit: 6000/3000 MS (Java/Other ...

  5. hduoj 4706 Herding 2013 ACM/ICPC Asia Regional Online —— Warmup

    hduoj 4706 Children's Day 2013 ACM/ICPC Asia Regional Online —— Warmup Herding Time Limit: 2000/1000 ...

  6. hduoj 4707 Pet 2013 ACM/ICPC Asia Regional Online —— Warmup

    http://acm.hdu.edu.cn/showproblem.php?pid=4707 Pet Time Limit: 4000/2000 MS (Java/Others)    Memory ...

  7. hduoj 4706 Children&#39;s Day 2013 ACM/ICPC Asia Regional Online —— Warmup

    http://acm.hdu.edu.cn/showproblem.php?pid=4706 Children's Day Time Limit: 2000/1000 MS (Java/Others) ...

  8. ACM ICPC Central Europe Regional Contest 2013 Jagiellonian University Kraków

    ACM ICPC Central Europe Regional Contest 2013 Jagiellonian University Kraków Problem A: Rubik’s Rect ...

  9. 2019-2020 ICPC, Asia Jakarta Regional Contest (Online Mirror, ICPC Rules, Teams Preferred)

    2019-2020 ICPC, Asia Jakarta Regional Contest (Online Mirror, ICPC Rules, Teams Preferred) easy: ACE ...

  10. 2013 ACM/ICPC Asia Regional Changsha Online - C Color Representation Conversion

    这个纯粹是一个细节题啊!!! 由于某个地方的浮点数比较写错了,WA了无数次啊…… 代码如下: #include<iostream> #include<cstdio> #incl ...

随机推荐

  1. openstack(liberty):部署实验平台(一,基础网络环境搭建)

    openstack项目的研究,到今天,算是要进入真实环境了,要部署实验平台了.不再用devstack了.也就是说,要独立controller,compute,storage和network了.要做这个 ...

  2. jquery 整理之一

    1.选择器: jQuery 属性选择器 jQuery 使用 XPath 表达式来选择带有给定属性的元素. $("[href]") 选取所有带有 href 属性的元素. $(&quo ...

  3. 剑指offer系列40----机器人的运动范围

    package com.exe8.offer; /** *[题目]地上有一个m行和n列的方格.一个机器人从坐标0,0的格子开始移动, * 每一次只能向左,右,上,下四个方向移动一格,但是不能进入行坐标 ...

  4. Centos7 + Windows7 双系统

    以前装双系统只要先装Windows7,然后再装Centos7的话,grub会自动添加原有的Windows7系统.不过在新的Centos7中需要手动修改. 步骤如下 $ sudo vi /etc/gru ...

  5. C语言每日一题之No.2

    题目:已知三个整型数8,12,6,按公式s=a+b*c计算,并显示结果 思路:定义三个整型变量a,b,c 定义一个变量s用来保存运算结果 输出 程序: #include <stdio.h> ...

  6. css hack 总结 包括ie6-11,chrome,opera,firefox

    <!DOCTYPE html> <html> <head> <title>Css Hack ie各版本 opera chrome safari fire ...

  7. Yii集成smarty说明

    1.       [在protected目录下建立文件夹vendor/smarty,把smarty的类包放入其中] 2.       [在extensions目录下边建立文件CSmarty.php] ...

  8. Well, let's start everything from the very beginning.

    帝都的霾仿佛亘古不变,不知觉2015年竟已快到尾声,而我在IBM也已呆了4个月.回顾过往多遇贵人,所获颇丰.最幸运的还是自己不忘初心,仍在不断成长.继续学习. 过去的几个月一直用WordPress,搭 ...

  9. OAF_MDS系列1_OAF页面元数据结构MDS的解析(概念)

    2014-06-06 Created By BaoXinjian

  10. CF 369C . Valera and Elections tree dfs 好题

    C. Valera and Elections   The city Valera lives in is going to hold elections to the city Parliament ...