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
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3726
题目大意:打印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..
思路:贪心的预处理下,然后二分查找取较小值即可。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#define MIN(a,b) ((a<b)?(a):(b))
using namespace std;
long long s[100010], p[100010], q, dp[100010];
int m, n;
int main() {
int t;
while(~scanf("%d", &t)) {
while(t--) {
scanf("%d%d", &n, &m);
for(int i = 0; i < n; i++) {
scanf("%lld%lld", &s[i], &p[i]);
}
memset(dp, 0, sizeof(dp));
dp[n - 1] = s[n - 1] * p[n - 1];
for(int i = n - 2; i >= 0; i--) {
dp[i] = min(dp[i + 1], s[i] * p[i]);
}
for(int i = 0; i < m; i++) {
scanf("%lld", &q);
if(q >= s[n - 1])
printf("%lld\n", q * p[n - 1]);
else {
long long pos = upper_bound(s, s + n, q) - s;
printf("%lld\n",min((q * p[pos - 1]), dp[pos]));
}
}
}
}
return 0;
}