2013 ACM/ICPC 长沙现场赛 A题 - Alice'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 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)

时间: 2024-08-02 06:55:02

2013 ACM/ICPC 长沙现场赛 A题 - Alice's Print Service (ZOJ 3726)的相关文章

2013 ACM/ICPC 长沙现场赛 C题 - Collision (ZOJ 3728)

Collision Time Limit: 2 Seconds      Memory Limit: 65536 KB      Special Judge There's a round medal fixed on an ideal smooth table, Fancy is trying to throw some coins and make them slip towards the medal to collide. There's also a round range which

hdu 4435 第37届ACM/ICPC天津现场赛E题

转载请注明出处,谢谢http://blog.csdn.net/ACM_cxlove?viewmode=contents    by---cxlove 题目:给出N个城市,从1开始需要遍历所有点,选择一些点建立加油站,使得花费最少 这题的特殊性在于他的花费上,2^(i-1) 利用一个非常重要的性质,2^0+2^1+2^2……+2^i<2^(i+1) 所有编号<=i的所有点都建,总花费比建一个还少. 这里就贪心一下,先假设所有点都建,然后依次从编号大的删点,看看能不能遍历整个图 dist[i]表示

hdu 4432 第37届ACM/ICPC天津现场赛B题

题目大意就是找出n的约数,然后把约数在m进制下展开,各个数位的每一位平方求和,然后按m进制输出. 模拟即可 1 #include<cstdio> 2 #include<iostream> 3 #include<algorithm> 4 #include<cstring> 5 #include<cmath> 6 #include<queue> 7 #include<map> 8 using namespace std; 9

HDU-4464-Browsing History (2012 ACM/ICPC成都现场赛!)

Browsing History Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3065    Accepted Submission(s): 1692 Problem Description One day when you are going to clear all your browsing history, you come

ACM/ICPC2015上海现场赛B题题解

给你一棵完全二叉树,初始能量为0,根节点编号为1(也就是说最左边那条路上节点的编号分别是2^0,2^1,2^2…2^(h-1)).从根节点开始往下走k-1步,走到每个节点选择加上或减去这个节点的编号,问走完这k个节点时能量恰好为n的方案.Special Judge. 在队友的提醒下(TAT我真是讨厌鹰语)看明白了数据范围,很明显是一个考二进制性质的题.于是一上手我先把样例改成了走最左路线的情况.样例出的蛮良心,一个奇数一个偶数,刚好对应两条路线:一直走左儿子或者走k-2个左儿子最后一个走右儿子.

HDU-4472-Count (2012 ACM/ICPC成都现场赛)

Count Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1850    Accepted Submission(s): 1200 Problem Description Prof. Tigris is the head of an archaeological team who is currently in charge of a

2014年ACM牡丹江赛区现场赛K题(ZOJ 3829)

Known Notation Time Limit: 2 Seconds      Memory Limit: 65536 KB Do you know reverse Polish notation (RPN)? It is a known notation in the area of mathematics and computer science. It is also known as postfix notation since every operator in an expres

HDU 4791 Alice&#39;s Print Service(2013长沙区域赛现场赛A题)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4791 解题报告:打印店提供打印纸张服务,需要收取费用,输入格式是s1 p1 s2 p2 s3 p3...表示打印区间s1到s2张纸的单价是p1,打印区间s2 到s3的单价是p2....最后是sn到无穷大的单价是pn,让你求打印k张纸的总费用最少是多少?有m次查询. 因为s1*p1 > s2 * p2 > s3*p3......,很显然,加入k所在的那个区间是第x个区间,那么最低费用要么是k * p

CUGBACM_Summer_Tranning3 2013长沙现场赛(二分+bfs模拟+DP+几何)

A题:二分 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4791 用lower_bound可以轻松解决,不过比赛的时候逗逼了. 刚开始没有预处理,所以队友给出一组数据的时候没通过,然后一时紧张又想不出什么好的解决办法,所以就没再继续敲代码.实在有点可惜了. #include<iostream> #include<cstring> #include<cstdio> #include<cmath> #includ