POJ_1260_Pearls_动态规划

人生不当平平淡淡。


题意:

一个公司卖的珍珠分为c个档次,价格从低到高都不相同,且同一档次的所有珍珠价格相同,由于经济衰退,每买一种珍珠都必须多花10个该档次珍珠的钱,同时,你可以把低档次的珍珠当成高档次的珍珠购买,以此避免购买低档次珍珠必须多花的10个珍珠的价格。已知你需要采购的各种珍珠的数量,求最少花费多少钱。



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.


典型DP,模型建立了好久才搞定,dp[i][j],i表示计算到了哪一种珍珠,j表示到当前珍珠种类为止有多少枚珍珠没有付款(即想要放到更高档次去付,包括当前种类),所以就有三种情况: 

  对0<j<a[i],dp[i][j]=inf;

  对j>=a[i],dp[i][j]=dp[i-1][j-a[i]](当前种类计算时有没付款的珍珠,则当前种类珍珠必然不付款,这是局部的贪心性质);

  对j==0,dp[i][j]=min(dp[i-1][j]+(j+10)*p[i]);

此题空间限制10^4KB,换算成int的数量是10^6级别,dp数组大小为c*c*a,刚好为10^7级别,上滚动数组解决。

代码如下:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
using namespace std;
#define mxn 110
#define mxm 100010
#define inf 0x3f3f3f3f
int a[mxn],p[mxn];
int n;
int dp[2][mxm];
int main(){
	int cs;
	scanf("%d",&cs);
	while(cs--){
		scanf("%d",&n);
		for(int i=1;i<=n;++i)
			scanf("%d%d",&a[i],&p[i]);
		memset(dp,0x3f,sizeof(dp));
		dp[0][0]=0;
		int sum=0;
		for(int i=1;i<=n;++i){
			sum+=a[i];
			for(int j=0;j<a[i];++j)
				dp[i%2][j]=inf;
			for(int j=a[i];j<=sum;++j)
				dp[i%2][j]=dp[(i+1)%2][j-a[i]];
			for(int j=1;j<=sum;++j)
				dp[i%2][0]=min(dp[i%2][0],dp[i%2][j]+(j+10)*p[i]);
		}
		printf("%d\n",dp[n%2][0]);
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值