码迷,mamicode.com
首页 > 其他好文 > 详细

zoj 3726 水题+二分

时间:2015-07-29 21:06:57      阅读:145      评论:0      收藏:0      [点我收藏+]

标签:

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

 

题目链接: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..

思路:贪心的预处理下,然后二分查找取较小值即可。

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cmath>
 5 #include <algorithm>
 6 #define MIN(a,b) ((a<b)?(a):(b))
 7 using namespace std;
 8 long long s[100010], p[100010], q, dp[100010];
 9 int m, n;
10 int main() {
11     int t;
12     while(~scanf("%d", &t)) {
13         while(t--) {
14             scanf("%d%d", &n, &m);
15             for(int i = 0; i < n; i++) {
16                 scanf("%lld%lld", &s[i], &p[i]);
17             }
18             memset(dp, 0sizeof(dp));
19             dp[n - 1] = s[n - 1] * p[n - 1];
20             for(int i = n - 2; i >= 0; i--) {
21                 dp[i] = min(dp[i + 1], s[i] * p[i]);
22             }
23             for(int i = 0; i < m; i++) {
24                 scanf("%lld", &q);
25                 if(q >= s[n - 1])
26                     printf("%lld\n", q * p[n - 1]);
27                 else {
28                     long long pos = upper_bound(s, s + n, q) - s;
29                     printf("%lld\n",min((q * p[pos - 1]), dp[pos]));
30                 }
31             }
32         }
33     }
34     return 0;
35 }

zoj 3726 水题+二分

标签:

原文地址:http://www.cnblogs.com/gaoxiang36999/p/4687235.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!