POJ 1260 Pearls

http://poj.org/problem?id=1260

题意:给出几类珍珠,以及它们要买的数量和单价,珍珠的质量依次上升,价格也依次上升,计算买所有珍珠需要花的最少价格。

购买规则是这样的,不管买哪一类的珍珠,最后都需要增加10个该类珍珠,并且质量低的珍珠可以用质量高的珍珠来代替。

      举个例子吧:

      100 1

      100 2

      如果正常买的话,就是(100+10)*1+(100+10)*2=330元,如果把第一类珍珠都按照第二类珍珠来买的话,需要(200+10)*2=440元。

思路:题意的要求主要是两点:

①购买的珍珠数量必须等于各类珍珠所要求数量的总和。

②珍珠的质量只可高,不可低。

动态规划思路是这样的:

我们依次一类类的来分析,用dp[i]来表示分析到第i类珍珠时所需要花的最少价钱。

状态转移方程为dp[i]=min(dp[i] ,dp[j]+(sum[i]-sum[j]+10)*price[i] )。   //枚举j

因为质量和价格都是依次上升的,所以如果低质量的珍珠要换成高质量的珍珠,肯定优先选择高一级的珍珠来代替,因为此时珍珠的价格比后来的珍珠便宜。

所以在状态转移方程之中,sum代表珍珠总和,sum[i]代表我们分析到第i类珍珠所需要购买的珍珠总数,sum[j](0<=j<i)代表将j~i类的珍珠都用第i类珍珠来代替。

最后输出dp[n]。

 1 #include<iostream>
 2 #include<algorithm>
 3 using namespace std;
 4
 5 const int maxn = 1000 + 5;
 6
 7 int n;
 8 int num[maxn], price[maxn];
 9 int dp[maxn];
10 int sum[maxn];
11
12 int main()
13 {
14     //freopen("D:\\txt.txt", "r", stdin);
15     int t;
16     cin >> t;
17     while (t--)
18     {
19         cin >> n;
20         sum[0] = 0;
21         for (int i = 1; i <= n; i++)
22         {
23             cin >> num[i] >> price[i];
24             sum[i] = sum[i - 1] + num[i];
25         }
26         dp[0] = 0;
27         for (int i = 1; i <= n; i++)
28         {
29             dp[i] = (num[i] + 10)*price[i] + dp[i - 1];   //未优化之前需要花多少钱
30             for (int j = 0; j < i; j++)
31             {
32                 dp[i] = min(dp[i], dp[j] + (sum[i] - sum[j] + 10)*price[i]);
33             }
34         }
35         cout << dp[n] << endl;
36     }
37     return 0;
38 }
时间: 2024-11-04 00:05:42

POJ 1260 Pearls的相关文章

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

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 ~\(≧▽≦)/~

点击打开链接 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

Pearls POJ 1260 DP

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

poj 1260 dp

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