HDU 4791 Alice's Print Service 简单DP

连接:http://acm.hdu.edu.cn/showproblem.php?pid=4791

题意:打印问题,n次条件,打印量≥si时,每张纸的打印价格为pi(0≤n≤1e5),问打印m次询问,qi张时最少需要多少钱(0≤m≤1e5)。

思路:如果对每次询问进行便利复杂度O(m*n)太大,超时。所以进行离线处理,将询问排序,从小到大依次处理,处理过程O(n+m),但排序过程是O(mlogm),所以总体的复杂度还是O(mlogm)。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <map>
#include <cstdlib>
#include <queue>
#include <stack>
#include <vector>
#include <ctype.h>
#include <algorithm>
#include <string>
#include <set>
#define PI acos(-1.0)
#define maxn 100005
#define INF 0x7fffffff
#define eps 1e-8
typedef long long LL;
typedef unsigned long long ULL;
using namespace std;
struct aa
{
    LL p,s;
    LL cost;
} a[100005];
struct bb
{
    LL i,q;
    LL cost;
} b[100005];
bool cmp1(bb a,bb b)
{
    return a.i<b.i;
}
bool cmp2(bb a,bb b)
{
    return a.q<b.q;
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        int m,n;
        scanf("%d%d",&m,&n);
        for(int i=0; i<m; i++)
        {
            scanf("%I64d%I64d",&a[i].p,&a[i].s);
        }
        a[m-1].cost=a[m-1].p*a[m-1].s;
        for(int i=m-2; i>=0; i--)
            a[i].cost=min(a[i+1].cost,a[i].p*a[i].s);
        for(int i=0; i<n; i++)
        {
            scanf("%I64d",&b[i].q);
            b[i].i=i;
        }
        sort(b,b+n,cmp2);
        int top1=0;
        for(int i=0; i<n; i++)
        {
            while(b[i].q>=a[top1].p&&top1<m)
                top1++;
            if(top1==m)
                b[i].cost=b[i].q*a[top1-1].s;
            else
                b[i].cost=min(b[i].q*a[top1-1].s,a[top1].cost);
        }
        sort(b,b+n,cmp1);
        for(int i=0; i<n; i++)
            printf("%I64d\n",b[i].cost);
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值