您的位置:首页 > 其它

HDU 4791 Alice's Print Service 简单DP

2014-07-29 11:29 471 查看
连接: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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  DP