您的位置:首页 > 其它

NYOJ 933 Bob's Print Service

2013-11-24 17:33 246 查看


Bob's Print Service

时间限制:1000 ms | 内存限制:65535 KB
难度:3

描述
Bob is providing print service,while the pricing doesn't seem to be reasonable,so people using her price service found some tricks to save money.

For example,the price when printing lwss than 100 pages is 20 cents per page,but when printing not less than 100 pages,you just need to pay only 10 cents 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.

输入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 <10^5).

The second line contains 2n integers S1,P1,S2,P2,……,Sn,Pn(0 = S1 < S2 < …… < Sn <= 10^9 ,

10^9 >= P1 >= P2 …… >= Pn >= 0).

The price when printing no less that S(i) but less that S(i+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 <= 10^9) are the queries.
输出For each query Qi,you should output the minimum amount of money(int cents) to pay if you want to print Qi pages, one output in one line.
样例输入
1
2 3
0 20 100 10
0 99 100


样例输出
0
1000
1000

题意:先给出两个整数n、m,然后给出n个区间的临界点和大于这个临界点的每张纸的价格。然后给出m个k,问打印纸张数不低于k的最小花费。

解题思路:先预处理一下,用一个数组v记录处理后的结果,v[i]表示打印张数不低于临界点s[i]时所花费的最小的钱数,然后二分查找出不低于要查找的纸张数的位置a,求出打印k张本该支付的钱数ans=k*p[a-1],将ans与记录的v[a]比较,求出的最小值即为最小花费。

#include<cstdio>
#include<algorithm>
using namespace std;
typedef long long LL;
const int N = 1e5 + 10;
LL s
, p
, v
;
int main()
{
    int t, m, n, i, k;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&m);
        for(i = 0; i < n; i++)
            scanf("%lld%lld",&s[i],&p[i]);
        for(i = n-1; i >= 0; i--)
        {
            if(i == n-1)
                v[i] = s[i] * p[i];
            else
                v[i] = min(v[i+1], s[i] * p[i]);
        }
        while(m--)
        {
            scanf("%d",&k);
            int a = upper_bound(s, s+n, k) - s;
            //二分查找,返回查找元素的最后一个可安插位置,即“元素值>查找值”的第一个元素的位置
            LL ans = k * p[a-1];
            if(a < n)
                ans = min(ans, v[a]);
            printf("%lld\n",ans);
        }
    }
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: