zoukankan      html  css  js  c++  java
  • poj 1260 dp

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

    2
    2
    100 1
    100 2
    3
    1 10
    1 11
    100 12

    Sample Output

    330
    1344

    题意:有c种品质不同的珠宝 ,若要买某一品质的珠宝必须支付(ai+10)*pi,可以用高品质的珠宝代替低品质的

    (通过减少购买的种类来节约多支付的10个的价钱).求要买到所有目标数量的珠宝至少要花多少钱

    题解:注意题目算价格的方式 注意只能由高品质代替低品质

    注意题目中 given in ascending order 珠宝的品质是升序给出的

    dp[i] 表示以第i个品质结尾的最优解

    dp[i]=min{dp[j-1]+(sum[i]-sum[j-1]+10)*p[i]} 

    (sum[i]-sum[j-1]+10)*p[i]表示第j种到第i种珍珠全部替换为第i种



     1 /******************************
     2 code by drizzle
     3 blog: www.cnblogs.com/hsd-/
     4 ^ ^    ^ ^
     5  O      O
     6 ******************************/
     7 //#include<bits/stdc++.h>
     8 #include<iostream>
     9 #include<cstring>
    10 #include<cstdio>
    11 #include<map>
    12 #include<algorithm>
    13 #include<cmath>
    14 #define ll long long
    15 #define PI acos(-1.0)
    16 #define mod 1000000007
    17 using namespace std;
    18 int t;
    19 int n;
    20 int cost[105];
    21 int qu[105];
    22 int sum[105];
    23 int dp[105];
    24 int main()
    25 {
    26     scanf("%d",&t);
    27     for(int i=1; i<=t; i++)
    28     {
    29         scanf("%d",&n);
    30         sum[0]=0;
    31         for(int j=1; j<=n; j++)
    32         {
    33             scanf("%d %d",&cost[j],&qu[j]);
    34             sum[j]=sum[j-1]+cost[j];
    35             dp[j]=1e9;
    36         }
    37         dp[0]=0;
    38         for(int j=1; j<=n; j++)
    39         {
    40             for(int k=1; k<=j; k++)
    41                 dp[j]=min(dp[j],dp[k-1]+(sum[j]-sum[k-1]+10)*qu[j]);
    42         }
    43         printf("%d
    ",dp[n]);
    44     }
    45     return 0;
    46 }


  • 相关阅读:
    Sqlserver 还原那些事
    Sqlserver CheckPoint 在三种恢复模式中的不同表现
    转 SQL Server中关于的checkpoint使用说明
    类继承中的 隐藏和重写的 区别
    转 关于C#中派生类调用基类构造函数的理解
    Sqlserver 笔记 持续更新
    Asp.net 执行回调操作后 无法更新ViewState的问题
    c# double decimal
    table用模板生成的问题
    MVC接收列表参数
  • 原文地址:https://www.cnblogs.com/hsd-/p/5726621.html
Copyright © 2011-2022 走看看