zoj 3726 水题+二分

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, 0, sizeof(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 }

时间: 2024-10-22 12:13:55

zoj 3726 水题+二分的相关文章

hdu - 6276,2018CCPC湖南全国邀请赛A题,水题,二分

题意: 求H的最大值,  H是指存在H篇论文,这H篇被引用的次数都大于等于H次. 思路:题意得,  最多只有N遍论文,所以H的最大值为N, 常识得知H的最小值为0. 所以H的答案在[0,N]之间,二分搜一下,如果满足就提高下限,不满足则降低上限. 嗯就这样!!!! AC code: #include<bits/stdc++.h> using namespace std; int n; vector<int> cc(200005); int main() { bool check(i

2013 ACM/ICPC 长沙现场赛 A题 - Alice&#39;s Print Service (ZOJ 3726)

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

ZOJ 3726 RMQ + 二分

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5072 区域赛果然无水题 通过率最高的一道题 以为二分下就OK  然后WA了果断 外加int没用long long WA 好久没用RMQ 调试也花了一点时间, upper--bound返回的是大于x的第一个数的下标,最大当然是返回end的位置,注意判断下 注意一点,假设需要打印的张数为x              s[i]=<x<s[i+1]   其实 要找的是ans=min

ZOJ 3609 Modular Inverse (水题)

Modular Inverse Time Limit: 2 Seconds      Memory Limit: 65536 KB The modular modular multiplicative inverse of an integer a modulo m is an integer x such that a-1≡x (mod m). This is equivalent to ax≡1 (mod m). Input There are multiple test cases. Th

ZOJ 3708 Density of Power Network (水题)

Density of Power Network Time Limit: 2 Seconds      Memory Limit: 65536 KB The vast power system is the most complicated man-made system and the greatest engineering innovation in the 20th century. The following diagram shows a typical 14 bus power s

水题 ZOJ 3875 Lunch Time

题目传送门 1 /* 2 水题:找排序找中间的价格,若有两个,选价格大的: 3 写的是有点搓:) 4 */ 5 #include <cstdio> 6 #include <iostream> 7 #include <algorithm> 8 #include <cmath> 9 #include <cstring> 10 #include <string> 11 #include <map> 12 #include <

水题 ZOJ 3876 May Day Holiday

题目传送门 1 /* 2 水题:已知1928年1月1日是星期日,若是闰年加1,总天数对7取余判断就好了: 3 */ 4 #include <cstdio> 5 #include <iostream> 6 #include <algorithm> 7 #include <cmath> 8 #include <cstring> 9 #include <string> 10 #include <map> 11 #include

hdu Courses 1083 二分匹配 ,匈牙利算法。。水题

Courses Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 4233    Accepted Submission(s): 2014 Problem Description Consider a group of N students and P courses. Each student visits zero, one or

POJ百道水题列表

以下是poj百道水题,新手可以考虑从这里刷起 搜索1002 Fire Net1004 Anagrams by Stack1005 Jugs1008 Gnome Tetravex1091 Knight Moves1101 Gamblers1204 Additive equations 1221 Risk1230 Legendary Pokemon1249 Pushing Boxes 1364 Machine Schedule1368 BOAT1406 Jungle Roads1411 Annive