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 题意: 打印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 0x7fffffff 22 #define maxn 100010 23 typedef unsigned long long ll; 24 25 int n, m; 26 struct price 27 { 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 else 78 { 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 }
注意事项:
1、_min数组的存在:
很多人遗忘了这种情况:0 40;
10 30;
20 20;
40 4;
若要打印15份,最少情况其实可以使打印40份(可打空白页),花费160元。这就是考虑_min数组的必要性;
2、二分法查找pos:注意因为题意要求,所以会有些小不同;
2013 ACM/ICPC 长沙现场赛 A题 - Alice's Print Service (ZOJ 3726)