dp斜率优化 Pearls(Hdu1300)题解

来源:互联网 发布:企业工单管理系统源码 编辑:程序博客网 时间:2024/06/06 00:58

累加器传送门:

http://blog.csdn.net/NOIAu/article/details/71775000

题目传送门:

https://vjudge.net/problem/HDU-1300


题目:

In Pearlania everybody is fond of pearls. One company, called The Royal Pearl, produces a lot of jewelry with pearls in it. The Royal Pearl has its name because it delivers to the royal family of Pearlania. But it also produces bracelets and necklaces for ordinary people. Of course the quality of the pearls for these people is much lower then the quality of pearls for the royal family. In Pearlania pearls are separated into 100 different quality classes. A quality class is identified by the price for one single pearl in that quality class. This price is unique for that quality class and the price is always higher then the price for a pearl in a lower quality class.

Every month the stock manager of The Royal Pearl prepares a list with the number of pearls needed in each quality class. The pearls are bought on the local pearl market. Each quality class has its own price per pearl, but for every complete deal in a certain quality class one has to pay an extra amount of money equal to ten pearls in that class. This is to prevent tourists from buying just one pearl.

Also The Royal Pearl is suffering from the slow-down of the global economy. Therefore the company needs to be more efficient. The CFO (chief financial officer) has discovered that he can sometimes save money by buying pearls in a higher quality class than is actually needed. No customer will blame The Royal Pearl for putting better pearls in the bracelets, as long as the prices remain the same.

For example 5 pearls are needed in the 10 Euro category and 100 pearls are needed in the 20 Euro category. That will normally cost: (5+10)*10 + (100+10)*20 = 2350 Euro.

Buying all 105 pearls in the 20 Euro category only costs: (5+100+10)*20 = 2300 Euro.

The problem is that it requires a lot of computing work before the CFO knows how many pearls can best be bought in a higher quality class. You are asked to help The Royal Pearl with a computer program.

Given a list with the number of pearls and the price per pearl in different quality classes, give the lowest possible price needed to buy everything on the list. Pearls can be bought in the requested, or in a higher quality class, but not in a lower one.


输入:

The first line of the input contains the number of test cases. Each test case starts with a line containing the number of categories c (1 <= c <= 100). Then, c lines follow, each with two numbers ai and pi. The first of these numbers is the number of pearls ai needed in a class (1 <= ai <= 1000). The second number is the price per pearl pi in that class (1 <= pi <= 1000). The qualities of the classes (and so the prices) are given in ascending order. All numbers in the input are integers.


输出:

For each test case a single line containing a single number: the lowest possible price needed to buy everything on the list.


样例输入:

2
2
100 1
100 2
3
1 10
1 11
100 12


样例输出:

330
1344


这道题是斜率优化的模板题(斜率优化有模板??)

题目大意是说,有C中品质不同的Pearls,每一种品质需要买ai个,每一个的价格为pi,而你支付的价格为(ai+10)*pi,现在可以用高品质的Pearl代替低品质的,求买到所有的目标Pearls至少需要花多少钱,品质递增,pi递增


这道题看到两个递增,很容易首先想到单调队列,或者是利用单调队列的斜率优化,对于斜率优化,我在这片博文里有较为详细的讲解,如果不懂斜率优化的,可以先看这篇博客
http://blog.csdn.net/NOIAu/article/details/71774994


现在进行分析,可以很容易地想出一个dp转移方程

dp[i]=min((cnt[i]-cnt[j]+10)*p[i]+dp[j])(1<=j<i);

其中cnt是前缀和,也就是前i中珍珠一共有多少个
dp[i]表示的是,把i及之前的珍珠以某种买法,所花的最少的钱
现在进行化简
很明显

dp[i]=min(dp[j]-cnt[j]*p[i])+cnt[i]*p[i]+10*p[i];

这里的是把和i有关的东西(可以O(1)计算出而与枚举j无关的数据)提出来
所以我们只需要求dp[j]-cnt[j]*p[i]的最小值就行了
自然而然想到斜率优化


令b=dp[i],x=cnt[j],y=dp[j],k=p[i]

原来的方程 dp[i]=dp[j]-cnt[j]*p[i]转化为了

b=y-kx

转换一下,移项得到

y=kx+b

所以用单调队列斜率优化,每次记录第几个点在队列中就行了,点的坐标为(x,y)
(如果看到这里没懂的请看推荐的那篇博客,因为这里我省略了如何维护单调队列,和斜率优化是什么的讲解)
传送门:
http://blog.csdn.net/NOIAu/article/details/71774994


直接上代码

#include<iostream>#include<cstring>#include<cstdio>#define MAXN 1000+10using namespace std;int T;int q[MAXN];int dp[MAXN];int cnt[MAXN];int p[MAXN];int head,tail;int n;void init(){    scanf("%d",&n);    //memset(dp,0,sizeof(0));    //memset(cnt,0,sizeof(0));    //memset(p,0,sizeof(0));    cnt[0]=0;    int temp;    for(int i=1;i<=n;i++){        scanf("%d",&temp);        cnt[i]=cnt[i-1]+temp;        scanf("%d",&p[i]);    }    head=1;tail=2;    dp[0]=0;    dp[1]=(cnt[1]+10)*p[1];    q[1]=0;    q[2]=1;}void dpp(){    for(int i=2;i<=n;i++){        while(head<tail&&dp[q[head]]-p[i]*cnt[q[head]]>dp[q[head+1]]-p[i]*cnt[q[head+1]]) head++;        dp[i]=dp[q[head]]-p[i]*cnt[q[head]]+cnt[i]*p[i]+10*p[i];        int x1=cnt[i]-cnt[q[tail-1]],y2=dp[q[tail]]-dp[q[tail-1]];        int x2=cnt[q[tail]]-cnt[q[tail-1]],y1=dp[i]-dp[q[tail-1]];        while(head<tail&&x1*y2-x2*y1>=0){            tail--;            x1=cnt[i]-cnt[q[tail-1]];y2=dp[q[tail]]-dp[q[tail-1]];            x2=cnt[q[tail]]-cnt[q[tail-1]];y1=dp[i]-dp[q[tail-1]];        }        q[++tail]=i;    }}void print(){    printf("%d\n",dp[n]);}int main(){    scanf("%d",&T);    while(T--){        init();        dpp();          print();    }    return 0;}

这里写图片描述

0 0
原创粉丝点击