zoukankan      html  css  js  c++  java
  • HDU1300 Pearls —— 斜率优化DP

    题目链接:https://vjudge.net/problem/HDU-1300

    Pearls

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 2920    Accepted Submission(s): 1464


    Problem 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
     
    Source
     
    Recommend
    Eddy

    题意: 

    进销珍珠。有n种珍珠,每种珍珠都有其需求量和价格。但是供货商要求,每一种珍珠(不管数量多少)都要附加购买10个这种类型的珍珠,或者一种较便宜的珍珠当成是较贵的珍珠,售价也如较贵的珍珠,这样就可以省去购买10个较便宜的珍珠。问:怎样购买才能以最低的花费完成进货任务。

    题解:

    1.首先可以得到一个结论:如果一种较便宜的珍珠要归到较贵的珍珠,那么这个较贵的珍珠必须选为刚好比较便宜珍珠贵一点点的(贪心的策略,既然都要归到较贵的了,那肯定要最便宜的那种。)

    2.把珍珠依照价格排序之后,其实就是一个区间的划分。可以直接O(n^2)枚举DP,也可以通过斜率进行优化:

    O(n^2):

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <algorithm>
     5 #include <vector>
     6 #include <cmath>
     7 #include <queue>
     8 #include <stack>
     9 #include <map>
    10 #include <string>
    11 #include <set>
    12 using namespace std;
    13 typedef long long LL;
    14 const int INF = 2e9;
    15 const LL LNF = 9e18;
    16 const int mod = 1e9+7;
    17 const int MAXM = 1e5+10;
    18 const int MAXN = 1e3+10;
    19 
    20 int val[MAXN], sum[MAXN], dp[MAXN];
    21 
    22 int main()
    23 {
    24     int T, n;
    25     scanf("%d", &T);
    26     while(T--)
    27     {
    28         scanf("%d", &n);
    29         sum[0] = 0;
    30         for(int i = 1; i<=n; i++)
    31             scanf("%d%d", &sum[i],&val[i]), sum[i] += sum[i-1];
    32 
    33         dp[0] = 0;
    34         for(int i = 1; i<=n; i++)
    35         {
    36             dp[i] = (sum[i]+10)*val[i];
    37             for(int j = 1; j<=i-1; j++)
    38                 dp[i] = min(dp[i], dp[j]+(sum[i]-sum[j]+10)*val[i]);
    39         }
    40         printf("%d
    ", dp[n]);
    41     }
    42 }
    View Code

    斜率优化:

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <algorithm>
     5 #include <vector>
     6 #include <cmath>
     7 #include <queue>
     8 #include <stack>
     9 #include <map>
    10 #include <string>
    11 #include <set>
    12 using namespace std;
    13 typedef long long LL;
    14 const int INF = 2e9;
    15 const LL LNF = 9e18;
    16 const int mod = 1e9+7;
    17 const int MAXM = 1e5+10;
    18 const int MAXN = 1e3+10;
    19 
    20 int val[MAXN], sum[MAXN], dp[MAXN];
    21 int q[MAXN], head, tail;
    22 
    23 int getUp(int i, int j)
    24 {
    25     return dp[i] - dp[j];
    26 }
    27 
    28 int getDown(int i, int j)
    29 {
    30     return sum[i] - sum[j];
    31 }
    32 
    33 int getDp(int i, int j)
    34 {
    35     return dp[j] + (sum[i]-sum[j]+10)*val[i];
    36 }
    37 
    38 int main()
    39 {
    40     int T, n;
    41     scanf("%d", &T);
    42     while(T--)
    43     {
    44         scanf("%d", &n);
    45         sum[0] = 0;
    46         for(int i = 1; i<=n; i++)
    47             scanf("%d%d", &sum[i],&val[i]), sum[i] += sum[i-1];
    48 
    49         dp[0] = 0;
    50         head = tail = 0;
    51         q[tail++] = 0;
    52         for(int i = 1; i<=n; i++)
    53         {
    54             while(head+1<tail && getUp(q[head+1],q[head])<=getDown(q[head+1],q[head])*val[i]) head++;
    55             dp[i] = getDp(i, q[head]);
    56             while(head+1<tail && getUp(i, q[tail-1])*getDown(q[tail-1],q[tail-2])<=
    57                   getUp(q[tail-1],q[tail-2])*getDown(i, q[tail-1]))   tail--;
    58             q[tail++] = i;
    59         }
    60         printf("%d
    ", dp[n]);
    61     }
    62 }
    View Code
  • 相关阅读:
    Spring IoC容器
    Spring Bean定义
    Spring框架 体系结构,一个简单的Spring程序
    Spring基础知识汇总 Java开发必看
    mybatis 优缺点(优点和缺点)
    MyBatis:<selectKey> #keyProperty、keyColumn、order
    fatal: The upstream branch of your current branch does not match the name of your current branch
    808端口被 OneApp.IGCC.WinService.exe占用的问题
    浅析如何使docker容器可以进行ssh连接
    浅析如何为正在运行的容器添加端口映射
  • 原文地址:https://www.cnblogs.com/DOLFAMINGO/p/8191635.html
Copyright © 2011-2022 走看看