Poj 1260 pearls [Dynamic Planning]

Source: Internet
Author: User

[Original question link]

Http://acm.pku.edu.cn/JudgeOnline/problem? Id = 1260

 

[Topic]

I want to buy several kinds of valuable pearls, but I have to pay 10 more for a pearl, and if I want to buy 100 pearls worth 1, I have to pay 110 yuan. A pearl can be filled with pearls that are more expensive than it. Therefore, it may be more cost-effective to replace them cheaply when you buy a variety of pearls. For example, if you buy 100 products with a value of 2 and 1 value of 1, then the best solution is to buy 101 products with a value of 2. Enter several types of pearls you want to buy, and you can fill them with high-price pearls to ask how much it will cost at least.

 

[Solution ideas]

We can break down the problem into an optimal sub-problem. If there is a pearl whose value is m to N, the maximum value of the pearl is K (M <k <= N) the process of obtaining the optimal solution is to enumerate the amount of money required for the Pearl bought with the last value plus the optimal solution for the Pearl bought with the last value to obtain the minimum value, this method is used to find the state transition equation of Dynamic Planning: ANS [I] = min (ANS [I], (sum [I]-sum [J] + 10) * P [I] + ans [J])

 

[SourceProgram]

[Code]

# Include <iostream>

Using namespace STD;

 

Long a [1100], p [1100], sum [1100], ANS [1100];

Int main ()

{

Long t, n, I, j;

Cin> T;

While (t --)

{

Cin> N;

Memset (sum, 0, sizeof (SUM ));

For (I = 1; I <= N; I ++)

{

Cin> A [I]> P [I];

Sum [I] = sum [I-1] + A [I];

// The sum array is used to record the sum, which facilitates the sum of values of a pearl in the future.

}

For (I = 1; I <= N; I ++)

{

Ans [I] = (sum [I] + 10) * P [I]; // only use the last value to buy the expected number of pearls.

For (j = 1; j <I; j ++) // use the state transition equation

Ans [I] = min (ANS [I], (sum [I]-sum [J] + 10) * P [I] + ans [J]);

}

Cout <ans [N] <Endl; // solution obtained by the output

}

Return 0;

}

[/Code]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.