hdoj 4791 Alice's Print Service(二分)

题意:现在你要打印一些东西,比如需要99张纸,打印100张以下时话费10元每张,100张及100张以上时需要5元每张,此时你可以选择打印100张,使得花费更小。现给一个数字n,表示n个区间段,然后有s1,p1,s2,p2......sn,pn,表示打印纸张大于等于s1而小于s2时,每张纸话费p1元,现有m个询问,问每次给你x张纸,所需的最小花费是多少。


思路:可以从后往前做一个O(n)的预处理,表示需要的纸张少于si时,多打印纸需要的最小花费。从后往前,则min[ si ] = min ( si*pi , min[ si+1 ] ),之后每次查询只需比较min[si]和正常方法的话费取最小即可。


在青理邀请赛的时候,推了挺久。。基本已经和答案一样了,然而还是错了一点点小细节 导致没过。。


代码:

#include<iostream>
#include<algorithm>
#include<cstdio>
using namespace std;
const int maxn = 1e5+5;
typedef long long ll;
ll s[maxn], p[maxn], minCost[maxn];

int main(void)
{
    int t;
    cin >> t;
    while(t--)
    {
        int n, m;
        scanf("%d%d", &n, &m);
        for(int i = 0; i < n; i++)
            scanf("%lld%lld", &s[i], &p[i]);
        minCost[n-1] = s[n-1]*p[n-1];
        for(int i = n-2; i >= 0; i--)
        {
            ll temp = s[i]*p[i];
            minCost[i] = min(temp, minCost[i+1]);
        }
        while(m--)
        {
            int x;
            scanf("%d", &x);
            int index = upper_bound(s, s+n, x)-s;
            index--;
            ll ans;
            if(index == n-1) ans = x*p[n-1];
            else ans = min(minCost[index+1], x*p[index]);
            printf("%lld\n", ans);
        }
    }
    return 0;
}



比赛时敲得:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = 1e5+5;
typedef long long ll;
ll s[maxn], p[maxn], n, m, num, newS[maxn], newP[maxn];
 
int main(void)
{
    int t;
    scanf("%d", &t);
    while(t--)
    {
        cin >> n >> m;
//        scanf("%I64d%I64d", &n, &m);
        for(int i = 1; i <= n; i++)
            scanf("%I64d%I64d", &s[i], &p[i]);
 
        ll k = 1;
        for(int i = n; i >= 1; i--)
        {
            if(k == 1)
            {
                newS[k] = s[i];
                newP[k++] = p[i];
            }
            else
            {
                if(s[i]*p[i] < newS[k-1]*newP[k-1])
                {
                    newS[k] = s[i];
                    newP[k++] = p[i];
                }
            }
        }
 
//            scanf("%I64d%I64d", &s[i], &p[i]);
        k--;
        n = k;
        for(int i = 1; i <= n/2; i++)
        {
            swap(newS[i], newS[n-i+1]);
            swap(newP[i], newP[n-i+1]);
        }
//        for(int i = 1; i <= n; i++)
//            printf("%I64d %I64d\n", newS[i], newP[i]);
        for(int i = 0; i < m; i++)
        {
            ll q;
            scanf("%I64d", &q);
//            scanf("%I64d", &q);
            ll l = 1, r = k, rec;
            while(l <= r)
            {
                ll mid = (l+r)/2;
                if(q == newS[mid])
                {
                    rec = mid;
                     break;
                }
                if(q > newS[mid]) l = mid+1, rec = mid;
                else r = mid-1;
            }
//            cout << rec << endl;
            ll ans = q*newP[rec];
            if(rec+1 <= n)
            {
                ll temp = newP[rec+1]*newS[rec+1];
                ans = min(ans, temp);
            }
            printf("%lld\n", ans);
//            printf("%I64d\n", ans);
        }
    }
    return 0;
}




Time Limit: 1000MS Memory Limit: 32768KB 64bit IO Format: %I64d & %I64u

 Status

Description

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 ≤ 10  5 ). The second line contains 2n integers s  1, p  1 , s  2, p  2 , ..., s  n, p  n (0=s  1 < s  2 < ... < s  n ≤ 10  9 , 10  9 ≥ p  1 ≥ p  2 ≥ ... ≥ p  n ≥ 0).. The price when printing no less than s  i but less than s  i+1 pages is p  i cents per page (for i=1..n-1). The price when printing no less than s  npages is p  n cents per page. The third line containing m integers q  1 .. q  m (0 ≤ q  i ≤ 10  9 ) are the queries.

Output

For each query q  i, you should output the minimum amount of money (in cents) to pay if you want to print q  i pages, one output in one line.

Sample Input

1
2 3
0 20 100 10
0 99 100

Sample Output

0
1000
1000

Source



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值