[Description ]:
Input:
2220 254018
Output:
The data here is represented in sequence as follows: the first 2 represents two groups of data, and the second represents two people. If you buy a ticket, the first person will spend 20 s, the other person will spend 25 s. If the two buy together, it will take 40 s (note that the two buy together must be two matching talents ), because the question is how long is the shortest time, enter 40 s. The specific time is:
08:00:40 am
The other one will not go into details.
[Analysis]: for the shortest time and the optimal solution, we can easily establish a state transition equation:
DP [I] = min (DP [I-1] + SS [I], DP [I-2] + dd [I-1]) // represents the time spent on a single purchase, respectively, the other is the time it takes to buy with others.
Code, is others' high quality code: http://www.cnblogs.com/Griselda/archive/2013/06/05/3118892.html
#include <stdio.h>#include <cstring>#include <algorithm>using namespace std;const int MAXN = 2010;int main(){ int T, n; int d[MAXN], s[MAXN], dp[MAXN] = {0}; int hh, mm, ss; scanf("%d", &T); while (T--) { scanf("%d", &n); for (int i = 1; i <= n; ++i) scanf("%d", &s[i]); for (int i = 2; i <= n; ++i) scanf("%d", &d[i]); dp[1] = s[1]; for (int i = 2; i <= n; ++i) dp[i] = min(dp[i-1]+s[i], dp[i-2]+d[i]); hh = dp[n]/3600; mm = dp[n]%3600/60; ss = dp[n]%60; printf("%02d:%02d:%02d%s\n", (8+hh)%24, mm, ss, (hh+8)%24>12?" pm":" am"); } return 0;}
HDU 1260 tickets (simple DP)