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 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

题意:题意比较难以解释,大概就是要用最小的花费去买完所有珍珠,珍珠可以分块购买(每次购买的价格是最高级的珍珠的价格)或者单类购买,每一次购买都要加上10个此次购买的最高级珍珠的价格(最高级的珍珠是输入时越靠后的,并不是最贵的,千万注意)
 1 #include <cstdio>
 2 #include <algorithm>
 3 #include <cstring>
 4 #include <cmath>
 5 #include <iostream>
 6 using namespace std;
 7 #define MAXN 1010
 8
 9 struct node
10 {
11     int a,p;
12 }c[MAXN];
13 int dp[MAXN],sum[MAXN];
14
15 bool cmp(node a,node b)
16 {
17     return a.p<b.p;
18 }
19
20 int main()
21 {
22     int t;
23     scanf("%d",&t);
24     while(t--){
25         int n;
26         scanf("%d",&n);
27         memset(dp,0,sizeof(dp));
28         memset(sum,0,sizeof(sum));
29         for(int i=1;i<=n;i++){
30             scanf("%d%d",&c[i].a,&c[i].p);
31             sum[i]=sum[i-1]+c[i].a;
32         }
33 //        sort(c+1,c+n+1,cmp);
34         int res,tmp,ans=0;
35         for(int i=1;i<=n;i++){
36             tmp=100000000;
37             for(int j=1;j<=i;j++){
38                 tmp=min( dp[j-1]+(sum[i]-sum[j-1]+10)*c[i].p,tmp );
39             }
40             dp[i]=tmp;
41         }
42         printf("%d\n",dp[n]);
43     }
44     return 0;
45 }

				
时间: 2024-10-20 01:21:28

POJ 1260:Pearls(DP)的相关文章

HDU 1260:Tickets(DP)

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

POJ 2192 :Zipper(DP)

恢复 DP一直以来都没怎么看,觉得很难,也只会抄抄模板和处理一些简单的背包.于是比赛出现了这道比较简单的题还是不会,要开始去学学DP了. 题意:给出3个字符串,分别为A串和B串还有C串,题目保证A的长度+B的长度=C的长度,C里面包含着A和B的字符,然后判断A和B在C里面的字符顺序还是不是和原来的A和B保持原样. 查看题目 做法:建立一个DP数组dp[i][j],第一维i表示使用了A的前i个字符,第二维j表示使用了B的前j个字符,然后赋予1或者0判断是否可以组成.如果此时c[i+j]==a[i]

POJ 2948 Martian Mining(DP)

题目链接 题意 : n×m的矩阵,每个格子中有两种矿石,第一种矿石的的收集站在最北,第二种矿石的收集站在最西,需要在格子上安装南向北的或东向西的传送带,但是每个格子中只能装一种传送带,求最多能采多少矿. 思路 :记忆化搜索.也可以用递推. //2948 #include <stdio.h> #include <string.h> #include <iostream> using namespace std ; int yeye[510][510] ,blog[510]

hdu 1300 Pearls (dp)

题目大意: 多种珍珠,每次选购都要在原有的数量上加上10. 例如:买5个单价是10的珍珠.需要的花费是(5+10)*10= 150.买100个单价是20的珍珠 需要的花费是(100+10)*20= 2200.总共需要的花费是150+2200=2350.如果把珍珠的质量提高了.需要的105个 珍珠都买单价是20的.也就是说都买质量好的.总的花费是(5+100+10)*20= 2300.在两组数据看来.珍珠都 买了高品质的了,而且花费也少了! 问题是怎么样能花费最少买珍珠! 思路分析: dp [i]

POJ 3280 Cheapest Palindrome(DP)

题目链接 题意 :给你一个字符串,让你删除或添加某些字母让这个字符串变成回文串,删除或添加某个字母要付出相应的代价,问你变成回文所需要的最小的代价是多少. 思路 :DP[i][j]代表的是 i 到 j 这一段位置变成回文所需的最小的代价. 1 //3280 2 #include <stdio.h> 3 #include <string.h> 4 #include <iostream> 5 6 using namespace std ; 7 8 char sh[2100]

【POJ 3071】 Football(DP)

[POJ 3071] Football(DP) Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4350   Accepted: 2222 Description Consider a single-elimination football tournament involving 2n teams, denoted 1, 2, -, 2n. In each round of the tournament, all tea

【POJ 3034】 Whac-a-Mole(DP)

[POJ 3034] Whac-a-Mole(DP) Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 3621   Accepted: 1070 Description While visiting a traveling fun fair you suddenly have an urge to break the high score in the Whac-a-Mole game. The goal of the W

Codeforces Gym101341K:Competitions(DP)

http://codeforces.com/gym/101341/problem/K 题意:给出n个区间,每个区间有一个l, r, w,代表区间左端点右端点和区间的权值,现在可以选取一些区间,要求选择的区间不相交,问最大的权和可以是多少,如果权和相同,则选区间长度最短的.要要求输出区间个数和选了哪些区间. 思路:把区间按照右端点排序后,就可以维护从左往右,到p[i].r这个点的时候,已经选择的最大权和是多少,最小长度是多少,区间个数是多少. 因为可以二分找右端点小于等于当前区间的左端点的某个区间

poj - 1093 - Formatting Text(dp)

题意:输入一段短文(所有字符总数不超过10000),要求格式化成两端对齐(每行长度为n,1 <= n <= 80)的方式输出并使得总坏值最小(一个空隙的坏值是这个空隙的空格总数减1后的平方),若有多种方案输出空格数字典序最小方案. 题目链接:http://poj.org/problem?id=1093 -->>状态:dp[i]表示从第i个单词开始到最后一个单词的最小总坏值(第i个单词是这一行的第1个单词) 状态转移方程:dp[i] = min(dp[i], dp[j + 1] +