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 integerT(≈ 10) which is the number of test cases. ThenTCases follow.

Each case contains 3 lines. The first line contains two integersN, m(0 <N, m≤ 105). The second line contains2NIntegersS1, P1, S2, P2,..., Sn, Pn(0 = S1 <S2 <... <Sn ≤ 109,109 ≥ P1 ≥ P2 ≥... ≥ PN ≥ 0). The price when printing no lessSiBut lessSi + 1Pages isPiCents per page (for I = 1... N-1). The price when printing no lessSnPages isPnCents per page. The third line containingMIntegersQ1... QM(0 ≤Qi≤ 109) are the queries.

Output

For each queryQi, You shoshould output the minimum amount of money (in cents) to pay if you want to printQiPages, one output in one line.

Sample Input

12 30 20 100 100 99 100

Sample output

010001000

题意： 打印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代码：

1 #include <iostream> 2 #include <sstream> 3 #include <cstdio> 4 #include <cstring> 5 #include <cmath> 6 #include <string> 7 #include <vector> 8 #include <set> 9 #include <cctype>10 #include <algorithm>11 #include <cmath>12 #include <deque>13 #include <queue>14 #include <map>15 #include <stack>16 #include <list>17 #include <iomanip>18 19 using namespace std;20 21 #define INF 0x7fffffff22 #define maxn 10001023 typedef unsigned long long ll;24 25 int n, m;26 struct price27 {28 int s, p;29 ll sum;30 }a[maxn];31 ll _min[maxn];32 int BinarySearch(int x)33 {34 int l = 0, r = n;35 int mid = l + (r-l)/2;36 while(l < r)37 {38 if(a[mid].s < x) {39 if(a[mid+1].s > x) return mid;40 l = mid;41 }42 else if(a[mid].s > x) {43 if(a[mid-1].s <= x) return mid-1;44 r = mid;45 }46 else return mid;47 mid = l + (r-l)/2;48 }49 return l;50 }51 52 int main()53 {54 int T;55 scanf("%d", &T);56 while(T--)57 {58 59 scanf("%d%d", &n, &m);60 for(int i = 0; i < n; i++){61 scanf("%d%d", &a[i].s, &a[i].p);62 a[i].sum = (ll)a[i].s * a[i].p;63 }64 _min[n-1] = a[n-1].sum;//注意此处的处理65 for(int i = n-2; i >= 0; i--)66 {67 _min[i] = min(_min[i+1], a[i].sum);68 }69 70 while(m--)71 {72 int num;73 ll ans = 0;74 scanf("%d", &num);75 if(a[n-1].s <= num)76 printf("%lld\n", (ll)num*a[n-1].p);77 else78 {79 int pos = BinarySearch(num);80 ans = (ll)num*a[pos].p;81 ans = min(_min[pos+1], ans);82 printf("%lld\n", ans);83 }84 }85 }86 return 0;87 }

View code

Note:

1. The _ min array exists:

Many people forget this situation: 0 40;

10 30;

20 20;

40 4;

If you want to print 15 copies, at least 40 copies can be printed (blank pages can be printed), and the cost is 160 yuan. This is the necessity to consider the _ min array;

2. Binary Search for POs: note that the meaning of the question is slightly different;

2013 ACM/ICPC Changsha field competition question a-Alice's Print Service (zoj 3726)