首页 > 代码库 > POJ 1260-Pearls(DP)

POJ 1260-Pearls(DP)

Pearls
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 7465   Accepted: 3695

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
水DP
题意: n件物品,给出数量和价格。(注意数量和价格都是升序给出的这个是能DP的关键)。要买掉所以商品 对于每类物品。所须要的价格是(a[i]+10)*p[i] ,即要多买10件,也能够把价格低的物品合并到高价格的物品那 即 (a[j]+a[i]+10)*p[j];代表把价格为i的物品合并到j上。设dp[i]为买完第i类物品的最优解
dp[i]=min(dp[i],dp[j]+(a[j+1]+a[j+2]+a[i]+10)*p[i]);
即对于第i类物品来说。它最多合并前面的i-1种物品,并且由于物品数目和价格都是升序的。所以能够证明仅仅要 第j类物品能合并,那么再往后就都能合并,由于第j类物品合并省出的钱能够表示为 a[j]*p[i]-(a[j]+10)*p[j] ;假设上式小于0,说明能够合并j,而当j增大,数目和价格都增大。所以仅仅会比j的时候更加省钱。即更能替换。
综上。有两种办法:
1.从前往后推,状态转移方程上面以给出。
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <string>
#include <cctype>
#include <vector>
#include <cstdio>
#include <cmath>
#include <deque>
#include <stack>
#include <map>
#include <set>
#define ll long long
#define maxn 116
#define pp pair<int,int>
#define INF 0x3f3f3f3f
#define max(x,y) ( ((x) > (y)) ? (x) : (y) )
#define min(x,y) ( ((x) > (y)) ? (y) : (x) )
using namespace std;
int n,a[110],p[110],dp[110],s[110];
int main()
{
	int T;
	scanf("%d",&T);
    while(T--)
	{
		scanf("%d",&n);
		s[0]=0;
		for(int i=1;i<=n;i++)
		{
			scanf("%d%d",&a[i],&p[i]);
			s[i]=s[i-1]+a[i];
		}
		dp[0]=0;
		for(int i=1;i<=n;i++)
		{
			dp[i]=dp[i-1]+(a[i]+10)*p[i];
			for(int j=0;j<i;j++)
				dp[i]=min(dp[i],dp[j]+(s[i]-s[j]+10)*p[i]);
		}
		printf("%d\n",dp[n]);
	}
	return 0;
}
2.从后往前推。由于对于最后一种状态n,要从前面的n-1种状态中倒着连续合并。
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <string>
#include <cctype>
#include <vector>
#include <cstdio>
#include <cmath>
#include <deque>
#include <stack>
#include <map>
#include <set>
#define ll long long
#define maxn 116
#define pp pair<int,int>
#define INF 0x3f3f3f3f
#define max(x,y) ( ((x) > (y)) ? (x) : (y) )
#define min(x,y) ( ((x) > (y)) ? (y) : (x) )
using namespace std;
int n,a[110],p[110],dp[110],s[110];
int dfs(int x)
{
	if(x<1)return 0;
	if(dp[x]>=0)return dp[x];
	if(x==1)return dp[x]=(a[x]+10)*p[x];
	dp[x]=(a[x]+10)*p[x]+dfs(x-1);
	for(int i=x-1;i>=1;i--)
		dp[x]=min(dp[x],dfs(i-1)+(s[x]-s[i-1]+10)*p[x]);
	return dp[x];
}
int main()
{
	int T;
	scanf("%d",&T);
	while (T--)
	{
		s[0]=0;
		memset(dp,-1,sizeof(dp));
		scanf("%d",&n);
		for(int i=1;i<=n;i++)
		{
			scanf("%d%d",&a[i],&p[i]);
			s[i]=s[i-1]+a[i];
		}
		printf("%d\n",dfs(n));
	}
	return 0;
}

   

POJ 1260-Pearls(DP)