Hdoj 1300 Pearls "DP"

Source: Internet
Author: User

PearlsTime limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 1699 Accepted Submission (s): 778


Problem DescriptionIn Pearlania Everybody is fond of pearls. One company, called the Royal Pearl, produces a lot of jewelry with pearls in it. The royal Pearl have 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 are much lower then the quality of pearls for the royal family. In Pearlania pearls is separated into different quality classes. A quality class is identified by the price of one single pearl in that quality class. This is the unique for, quality class and the price are always higher then the price of a pearl in a lower quality C Lass.

Every month the stock manager of the Royal Pearl prepares a list with the number of pearls needed in each quality class. The pearls is bought on the local pearl market. Each quality class have its own price per pearl, but for every complete deal in a certain quality class one have to pay an E Xtra Amount of money equal to ten pearls in the That class. 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 is more efficient. The CFO (Chief financial officer) has discovered so he can sometimes save money by buying pearls in a higher quality CLA SS than is actually needed. No customer would blame the Royal Pearl for putting better pearls in the bracelets, as long as the prices remain the same.

For example 5 pearls is needed in the euro category and pearls is needed in the euro category. That would normally cost: (5+10) *10 + (100+10) *20 = 2350 Euro.

Buying all pearls in the euro category only costs: (5+100+10) *20 = 2300 Euro.

The problem is, it requires a lot of computing work before the CFO knows what many pearls can best being bought in a Highe R Quality Class. You is asked to help the Royal Pearl and a computer program.

Given a list with the number of pearls and the price per pearl in different quality classes, give the lowest possible pric E needed to buy everything on the list. Pearls can is bought in the requested, or in a higher quality class, and not in a lower one.

Inputthe first line of the input contains the number of test cases. Each test case is starts with a line containing the number of categories C (1 <= C <= 100). Then, the C lines follow, each with the 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) is given in ascending order. All numbers in the input is integers.

Outputfor each test case a single line containing a single number:the lowest possible price needed to buy everything on t He list.

Sample Input
22100 1100 231 101 11100 12

Sample Output
3301344

Sourcenorthwestern Europe 2002

Test instructions: There are some quality (the higher the quality, the higher the price) different pearls, now need some different pearls, each quality different pearls have a certain price, and if you want to buy such pearls in the specified quality, then also need to buy 10 more, but low-quality pearls can be bought on high-quality pearls, such as Need to buy two kinds (5 (number), 10 (Price)), (100, 20), then the original will need to spend (5+10) *10+ (100+10) *20 = 2350, but there is another way to buy (5+100+10) *20 = 2300

With DP, state equation dp[i] = min (dp[j]+ (a[i]-a[j]+10) *p[i], dp[i]), Dp[i] represents the minimum amount of money before I,dp[j]+ (a[i]-a[j]+10) *p[i] Indicates that from the type J to the first I are purchased on the quality I, the minimum value can be (j; i);

Code:

#include <iostream> #include <cstdio> #include <cstring> #include <algorithm>const int M = 1e2+5; const int INF = 0x3f3f3f3f;using namespace Std;int dp[m], a[m], P[m];int main () {    int t;    scanf ("%d", &t);    while (T--) {        int n;        scanf ("%d", &n);        A[0] = 0;        int W;        for (int i = 1; I <= n; + + i) {            scanf ("%d%d", &w, p+i);            A[i] = a[i-1]+w;        }        memset (DP, 0, sizeof (DP));        int ans = INF;        for (int i = 1; I <= n; + + i) {            int Min = INF;            for (int j = 0; J < i; + + j) {                if (dp[j]+ (a[i]-a[j]+10) *p[i] < min) {                    min = dp[j]+ (a[i]-a[j]+10) *p[i];                }            }            Dp[i] = Min;            Ans  = min (ans, dp[i]);        }        printf ("%d\n", Dp[n]);    }    return 0;}


Hdoj 1300 Pearls "DP"

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.