HDU 1260 Tickets (simple DP)
[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.
#include
#include
#include 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, (8+hh)%24, mm, ss, (hh+8)%24>12? pm: am); } return 0;}