Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 8944 | Accepted: 4482 |
Description
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 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
Sample Input
2 2 100 1 100 2 3 1 10 1 11 100 12
Sample Output
330 1344
Source
根据这4个约束条件,那么购买珍珠的方案为:
在珍珠类型的总区间[1,c]中划分多个子区间,其中在闭区间i1~j1的珍珠全部按第j1类珍珠的价格p1支付,在闭区间i2~j2的珍珠全部按第j2类珍珠的价格p2支付,…在闭区间in~jn的珍珠全部按第jn类珍珠的价格pn支付。 这些区间互不相交。
其余珍珠按其原价支付。
要求找出最优的划分方案,使得最终支付价格最低。
令dp[i]表示在已知第i类珍珠时,所需支付的最低价格
则状态方程为:
dp[i]=(a[i]+10)*p[i]+dp[i-1]; //当第i种珍珠出现时,未优化价格的情况
dp[i]=min(dp[i],(sum[i]-sum[j]+10)*p[i]+dp[j]); //枚举j,价格优化
dp[0]=0; //Dp初始化
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
struct node
{
int id;
int ip;
}a[1100];
int sum[1100],DP[1100];
int main()
{
int test,c;
scanf("%d",&test);
while(test--)
{
memset(sum,0,sizeof(sum));
memset(DP,0,sizeof(DP));
int i,j;
scanf("%d",&c);
sum[0] = 0;
for(i = 1; i <= c; i++)
{
scanf("%d %d",&a[i].ip,&a[i].id);
sum[i] = sum[i-1]+a[i].ip;
}
/*DP*/
DP[0] = 0;
for(i = 1; i <= c; i++)
{
DP[i] = (a[i].id * (a[i].ip + 10)) + DP[i-1];
for(j = 0; j < i; j++)
{
DP[i] = min(DP[i],DP[j] + (sum[i]-sum[j]+10)*a[i].id);
}
}
printf("%d\n",DP[c]);
}
return 0;
}