Poj 1260-Pearls Dynamic Planning

Source: Internet
Author: User

Source: http: // 124.205.79.250/judgeonline/problem? Id = 1260

 

Solution report:

 

At first, I thought I could use greedy algorithms. Later I found it was wrong. For example, I tried the following test data:
10 10
5 15
10 23
It won't work. Still use dynamic planning

 

I set f [k] to buy only the first K, which can be the minimum cost.

Then
F [k] = min (A1 + A2 +... + aK + 10) * PK, F [1] + (A2 +... + aK + 10) * PK ,..., f [k-1] + (ak + 10) * PK)

Obviously, the final f [N] is required.

 

# Include <iostream> <br/> using namespace STD; </P> <p> int main () <br/>{< br/> int casenum; <br/> CIN> casenum; <br/> while (casenum --) <br/>{< br/> int N; <br/> CIN> N; <br/> int * A = new int [N]; <br/> int * P = new int [N]; <br/> int * sum = new int [N]; <br/> int * f = new int [N]; <br/> for (INT I = 0; I <n; I ++) <br/> CIN> A [I]> P [I]; <br/> sum [0] = A [0]; <br/> for (INT I = 1; I <n; I ++) <br/> sum [I] = A [I] + sum [I-1]; <br/> int min; <br/> F [0] = (a [0] + 10) * P [0]; <br/> for (INT I = 1; I <n; I ++) <br/> {<br/> min = (sum [I] + 10) * P [I]; <br/> for (Int J = 0; j <I; j ++) <br/> {<br/> int temp = f [J] + (sum [I]-sum [J] + 10) * P [I]; <br/> If (min> temp) <br/> min = temp; <br/>}< br/> F [I] = min; <br/>}< br/> cout <F [n-1] <Endl; <br/> Delete [] A; <br/> Delete [] P; <br/> Delete [] sum; <br/> Delete [] F; <br/>}< br/>}

 

Appendix:

Pearls
Time limit:1000 ms   Memory limit:10000 K
Total submissions:3231   Accepted:1520

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
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 can 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

22100 1100 231 101 11100 12

Sample output

3301344

Source

 

 

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.