POJ 1260 Pearls (动归)

Pearls

Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 7210 Accepted: 3543

Description

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.

Input

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.

Output

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

Sample Input

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

Sample Output

330
1344

题意解释:

有n个等级的珠宝,等级依次升高,等级越高价钱越高,每买一个等级的任何数量的珠宝必须多付10颗此种珠宝的价钱,可以用高等级的珠宝代替低等级的,问要买到若干规定的数量和等级珠宝的最少花费。例如买5颗价值为10的、100颗价值为20的珠宝,有两种方案:一种为分别买两种等级的珠宝价钱为(5+10)*10+(100+10)*20 = 2350;另一种是将等级低的(即价格低)的珠宝全部换为等级高的,此时价钱为(5+100+10)*20 = 2300,故第二种方案较优。

解题思路:

首先需要证明一点,用高一等级的代替低一等级的必须是连续代替。例如 3 5,90 7,100 12要想代替3个5价值的首先考虑用7价值的代替,因为3*7=21 (3+10)*5=65 显然比较合算,这样就不需要考虑用12价值的那个代替了,因为前面已经有了最优解,也就是说相互代替不会跳跃。

证明这一点后开始分析,用sum[i]表示前i中珠宝总数,对于第i等级珠宝,dp[i]表示其最优解,那么dp[i]可能是(sum[i]+10) *p[i] 即前面所有的珠宝用i的价值去买,或者(sum[i]-sum[1]+10)*p[i]+dp[1] 即从第二个开始前面的所有珠宝用i的价值去买,或者(sum[i]-sum[2] +10)*p[i] + dp[2] 即从第三个。。。依次计算,其中最小的一个就是i的解。注意,上面提到的跳跃是指不用i+1等级珠价值去计算i等级以及其以下的珠宝,但可以将i以下等级的珠宝归为等级为i的价格进行计算,这样就会保证当前价值最小。这样状态方程便有dp[i]=min(dp[i],(sum[i]-sum[j]+10)*p[i]+dp[j]),(1<=j<=i)。

在写状态方程时是整体到局部的思路去考虑,具体写代码特别是边界条件时则需要自底下向上推导。通常需要将状态数组(此题为dp[])分开处理,即先对其边界赋值,处理特殊情况,再按状态方程进行计算。
#include <stdio.h>
    #include <string.h>
    int tcase, n, a[2000], p[2000], dp[2000], sum[2000];  

    int min (int x, int y)
    {
        return x>y?y:x;
    }  

    int main ()
    {
        int i, j;
        scanf("%d", &tcase);
        while(tcase--)
        {
            memset(dp, 0, sizeof(dp));
            sum[0] = 0;
            scanf("%d", &n);
            for (i = 1; i <= n; i++)
            {
                scanf("%d%d", &a[i], &p[i]);
                sum[i] = sum[i-1] + a[i];
                dp[i] = (sum[i]+10) * p[i];
            } // 数组临界预处理。
            for (i = 1; i <= n; i++)
                for (j = 1; j <= i; j++)
                    dp[i] = min (dp[i], (sum[i]-sum[j]+10)*p[i] + dp[j]);
                // 根据状态转移方程计算数组。
                // 此处取最小值的方法较巧妙,值得学习。
                printf("%d\n", dp[n]);
        }
        return 0;
    }  

POJ 1260 Pearls (动归)

时间: 2024-11-06 07:20:22

POJ 1260 Pearls (动归)的相关文章

POJ 1260 Pearls 简单dp

1.POJ 1260 2.链接:http://poj.org/problem?id=1260 3.总结:不太懂dp,看了题解 http://www.cnblogs.com/lyy289065406/archive/2011/07/31/2122652.html 题意:珍珠,给出需求,单价,要求用最少的钱就可以买到相同数量的,相同(或更高)质量的珍珠. 把握题意,1.输入时,后输入的珍珠价格一定比前面输入的要贵.2.用高质量珍珠替代低质量. #include<iostream> #include

poj 1260 Pearls ( 区间dp )

链接:poj 1260 题意:给出n类珍珠,所需它们的数量,以及它们的单价, 要求用最少的钱就可以买到相同数量的,相同(或更高)质量的珍珠. 注:价格更高的珍珠等级更高,支付规则为: 买任一类的珍珠n个(单价:p),都要支付(n+10)*p的钱 例如: 3 1 10 1 11 100 12 需要买第一类1个,第二类1个,第三类100个 按常规支付为 (1+10)*10 + (1+10)*11 + (100+10)*12 = 1551元 但是如果全部都按照第三类珍珠的价格支付,同样是买102个,

POJ 1260 Pearls

http://poj.org/problem?id=1260 题意:给出几类珍珠,以及它们要买的数量和单价,珍珠的质量依次上升,价格也依次上升,计算买所有珍珠需要花的最少价格. 购买规则是这样的,不管买哪一类的珍珠,最后都需要增加10个该类珍珠,并且质量低的珍珠可以用质量高的珍珠来代替.       举个例子吧:       100 1       100 2       如果正常买的话,就是(100+10)*1+(100+10)*2=330元,如果把第一类珍珠都按照第二类珍珠来买的话,需要(2

POJ 1260 Pearls ~\(≧▽≦)/~

点击打开链接 Pearls Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 7553   Accepted: 3738 Description 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 h

【POJ 1260】Pearls

[POJ 1260]Pearls dp问题 最近做背包做多了 一做动规就往背包想-- 这题其实也有点背包的意思(然而只是做背包做的看啥都像背包-- c件物品 有各自的数量a 和价值p 每进行一次交易的花费cost = (物品数+10)*价格 低价物品可以用高价一起购买 一次交易只能按照一种价值购买 初始dp[0] = 0 dp数组下标为物品件数 枚举物品种类 没枚举一种物品 遍历该物品之前物品量 假设之前有num件物品 当前枚举到的物品价值p 那么就要找到min(dp[k(0~num)] + (

POJ 1260:Pearls(DP)

http://poj.org/problem?id=1260 Pearls Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 8474   Accepted: 4236 Description In Pearlania everybody is fond of pearls. One company, called The Royal Pearl, produces a lot of jewelry with pearls

poj 1260

K - Pearls Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 1260 Appoint description:  System Crawler  (2014-11-12) Description In Pearlania everybody is fond of pearls. One company, called The R

POJ 2955 Brackets (动规)

Brackets Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2999   Accepted: 1536 Description We give the following inductive definition of a "regular brackets" sequence: the empty sequence is a regular brackets sequence, if s is a reg

HDU 1260 Tickets (动规)

Tickets Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 924    Accepted Submission(s): 468 Problem Description Jesus, what a great movie! Thousands of people are rushing to the cinema. However,