dp simple example poj 1260 Pearls

poj 1260 PearlsClick  to open the link

After taking a few dp questions with the solution, I finally have some ideas for this question (don't spray the dp boss who is very watery).

The meaning of the question: Give the price of pearls and the quantity to be bought Count. A total of t groups of data, each group of n types of pearls. The price of pearls given first must not be greater than the price of pearls given later. For each additional type of pearls you buy, you have to pay more than the unit price of that type of pearls * 10. Pearls with low prices can be replaced by pearls with high prices. Ask for the lowest price after buying all the pearls.

Ideas: dp questions.

Stage: Each type of pearl is a stage.

Status: The minimum price required to purchase pearls from the first to this type.

Decision: For each type of pearl dp[i]=dp[i-1]+(Count[i]+10)*Price[i].

Then, take min with the previous decisions, namely dp[i]=min(dp[i],dp[j]+(sum[i]-sum[j]+10)*Price[i]);

where (sum[i]-sum[j]+10)*Price[i]) refers to the cost of replacing the j+1-th pearl with the i-th item.

(There is a pit point that the replacement of pearls can only be continuous. For example, pearls of level i+2 cannot replace pearls of level i, because if the price of level i+2 is better than that of level i, i+1 is replaced by level i. would be better.)

#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int Max=1e3+10;
int dp[Max];
int Price[Max],Count[Max];
int sum[Max];
intmain()
{
    int t,n;
    scanf("%d",&t);
    while(t--){
        memset(dp,0,sizeof dp);
        memset(sum,0,sizeof sum);
        scanf("%d",&n);
        for( int i=1;i<=n;i++){
            scanf("%d %d",&Count[i],&Price[i]);
            sum[i]=sum[i-1]+Count[i];
        }
        dp[0]=0;
        sum[0]=0;
        for(int i=1;i<=n;i++){
            dp[i]=(Count[i]+10)*Price[i]+dp[i-1];
            for(int j=0;j<i;j++){
                dp[i]=min(dp[i],dp[j]+(sum[i]-sum[j]+10)*Price[i]);
            }
        }
        printf("%d\n",dp[n]);
    }
    return 0;
}

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326608106&siteId=291194637