• HDU1260 Tickets —— DP


    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1260

    Tickets

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


    Problem Description
    Jesus, what a great movie! Thousands of people are rushing to the cinema. However, this is really a tuff time for Joe who sells the film tickets. He is wandering when could he go back home as early as possible.
    A good approach, reducing the total time of tickets selling, is let adjacent people buy tickets together. As the restriction of the Ticket Seller Machine, Joe can sell a single ticket or two adjacent tickets at a time.
    Since you are the great JESUS, you know exactly how much time needed for every person to buy a single ticket or two tickets for him/her. Could you so kind to tell poor Joe at what time could he go back home as early as possible? If so, I guess Joe would full of appreciation for your help.
     
    Input
    There are N(1<=N<=10) different scenarios, each scenario consists of 3 lines:
    1) An integer K(1<=K<=2000) representing the total number of people;
    2) K integer numbers(0s<=Si<=25s) representing the time consumed to buy a ticket for each person;
    3) (K-1) integer numbers(0s<=Di<=50s) representing the time needed for two adjacent people to buy two tickets together.
     
    Output
    For every scenario, please tell Joe at what time could he go back home as early as possible. Every day Joe started his work at 08:00:00 am. The format of time is HH:MM:SS am|pm.
     
    Sample Input
    2 2 20 25 40 1 8
     
    Sample Output
    08:00:40 am 08:00:08 am
     
    Source
     
     
    题解:
     
     
    原始做法:
    1.dp[i][j][status]表示:第i个人属于第j个组合,他的状态是status(是跟前一个人组队还是自己买)的最少花费时间。
    2.状态转移方程:
    dp[i][j][0] = dp[i-1][j][1] - a[i-1] + b[i-1];  //跟上一个人组队
    dp[i][j][1] = min(dp[i-1][j-1][0], dp[i-1][j-1][1]) + a[i]; //自己买
     
    代码如下:
     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <cmath>
     5 #include <algorithm>
     6 #include <vector>
     7 #include <queue>
     8 #include <stack>
     9 #include <map>
    10 #include <string>
    11 #include <set>
    12 #define ms(a,b) memset((a),(b),sizeof((a)))
    13 using namespace std;
    14 typedef long long LL;
    15 const double EPS = 1e-8;
    16 const int INF = 2e9;
    17 const LL LNF = 2e18;
    18 const int MAXN = 2e3+10;
    19 
    20 int dp[MAXN][MAXN][2], a[MAXN], b[MAXN];
    21 
    22 int main()
    23 {
    24     int T, n;
    25     scanf("%d", &T);
    26     while(T--)
    27     {
    28         scanf("%d", &n);
    29         for(int i = 1; i<=n; i++) scanf("%d", &a[i]);
    30         for(int i = 1; i<n; i++) scanf("%d", &b[i]);
    31 
    32         for(int i = 0; i<=n; i++)
    33         for(int j = 0; j<=n; j++)
    34             dp[i][j][0] = dp[i][j][1] = INF/2;
    35 
    36         dp[0][0][0] = 0;
    37         for(int i = 1; i<=n; i++)
    38         for(int j = (i+1)/2; j<=i; j++) //最少应该属于第(i+1)/2个组合
    39         {
    40             dp[i][j][0] = dp[i-1][j][1] - a[i-1] + b[i-1];  //跟上一个人组队
    41             dp[i][j][1] = min(dp[i-1][j-1][0], dp[i-1][j-1][1]) + a[i]; //自己买
    42         }
    43 
    44         int time = INF;
    45         for(int i = (n+1)/2; i<=n; i++)
    46             time = min(time, min(dp[n][i][0], dp[n][i][1]) );
    47 
    48         int second = time%60;
    49         int minute = (time/60)%60;
    50         int hour  = time/3600 + 8;
    51 
    52         int id = 0;
    53         if(hour>12){
    54             id = 1;
    55             hour = hour-12;
    56         }
    57         printf("%02d:%02d:%02d %s
    ", hour, minute, second, id?"pm":"am");
    58     }
    59 }
    View Code
     
     
    改进:
    1.后来发现规划第i个人属于第几个集合时多余的,所以: dp[i][status]表示:第i个人的状态是status的最少花费时间。
    2.状态转移方程:
    dp[i][0] = dp[i-1][1] - a[i-1] + b[i-1];    //跟上一个人组队
    dp[i][1] = min(dp[i-1][0], dp[i-1][1]) + a[i];  //自己买
    代码如下:
     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <cmath>
     5 #include <algorithm>
     6 #include <vector>
     7 #include <queue>
     8 #include <stack>
     9 #include <map>
    10 #include <string>
    11 #include <set>
    12 #define ms(a,b) memset((a),(b),sizeof((a)))
    13 using namespace std;
    14 typedef long long LL;
    15 const double EPS = 1e-8;
    16 const int INF = 2e9;
    17 const LL LNF = 2e18;
    18 const int MAXN = 2e3+10;
    19 
    20 int dp[MAXN][2], a[MAXN], b[MAXN];
    21 
    22 int main()
    23 {
    24     int T, n;
    25     scanf("%d", &T);
    26     while(T--)
    27     {
    28         scanf("%d", &n);
    29         for(int i = 1; i<=n; i++) scanf("%d", &a[i]);
    30         for(int i = 1; i<n; i++) scanf("%d", &b[i]);
    31 
    32         for(int i = 0; i<=n; i++)
    33             dp[i][0] = dp[i][1] = INF/2;
    34 
    35         dp[0][0] = 0;
    36         for(int i = 1; i<=n; i++)
    37         {
    38             dp[i][0] = dp[i-1][1] - a[i-1] + b[i-1];
    39             dp[i][1] = min(dp[i-1][0], dp[i-1][1]) + a[i];
    40         }
    41         int time = min(dp[n][0], dp[n][1]);
    42 
    43         int second = time%60;
    44         int minute = (time/60)%60;
    45         int hour  = time/3600 + 8;
    46 
    47         int id = 0;
    48         if(hour>12){
    49             id = 1;
    50             hour = hour-12;
    51         }
    52         printf("%02d:%02d:%02d %s
    ", hour, minute, second, id?"pm":"am");
    53     }
    54 }
    View Code
     
     
    再改进:
    1.再后来发现,前面两份代码都要用status来标记第i个人是否自己买,但是实际可以不用标记:如果第i个人自己买, 那么可以直接:dp[i] = dp[i-1] + a[i],如果跟别人组队买,那么 dp[i] = dp[i-2] + b[i-1],直接跳到前两个,免去了考虑前一个是否自己买。
    2.状态转移方程:
    dp[i] = min(dp[i-1]+a[i], dp[i-2]+b[i-1]);
      
    代码如下:
     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <cmath>
     5 #include <algorithm>
     6 #include <vector>
     7 #include <queue>
     8 #include <stack>
     9 #include <map>
    10 #include <string>
    11 #include <set>
    12 #define ms(a,b) memset((a),(b),sizeof((a)))
    13 using namespace std;
    14 typedef long long LL;
    15 const double EPS = 1e-8;
    16 const int INF = 2e9;
    17 const LL LNF = 2e18;
    18 const int MAXN = 2e3+10;
    19 
    20 int dp[MAXN], a[MAXN], b[MAXN];
    21 
    22 int main()
    23 {
    24     int T, n;
    25     scanf("%d", &T);
    26     while(T--)
    27     {
    28         scanf("%d", &n);
    29         for(int i = 1; i<=n; i++) scanf("%d", &a[i]);
    30         for(int i = 1; i<n; i++) scanf("%d", &b[i]);
    31 
    32         dp[0] = 0; dp[1] = a[1];
    33         for(int i = 2; i<=n; i++)
    34             dp[i] = min(dp[i-1]+a[i], dp[i-2]+b[i-1]);
    35 
    36         int second = dp[n]%60;
    37         int minute = (dp[n]/60)%60;
    38         int hour  = dp[n]/3600 + 8;
    39 
    40         int id = 0;
    41         if(hour>12){
    42             id = 1;
    43             hour = hour-12;
    44         }
    45         printf("%02d:%02d:%02d %s
    ", hour, minute, second, id?"pm":"am");
    46     }
    47 }
    View Code
  • 相关阅读:
    XI.spring的点点滴滴--IObjectFactoryPostProcessor(工厂后处理器)
    Ⅹ.spring的点点滴滴--IObjectPostProcessor(对象后处理器)
    Ⅸ.spring的点点滴滴--IObjectFactory与IFactoryObject的杂谈
    Ⅷ.spring的点点滴滴--抽象对象和子对象
    Ⅶ.spring的点点滴滴--自定义对象行为
    Ⅵ.spring的点点滴滴--自定义类型转换器
    Ⅴ.spring的点点滴滴--引用其他对象或类型的成员
    Ⅳspring的点点滴滴--方法和事件
    Ⅲ.spring的点点滴滴--赋值
    Ⅱ.spring的点点滴滴--对象
  • 原文地址:https://www.cnblogs.com/DOLFAMINGO/p/7624600.html
Copyright © 2020-2023  润新知