HDU-1260.Tickets(简单线性DP)
作者:互联网
本题大意:排队排票,每个人只能自己单独购买或者和后面的人一起购买,给出k个人单独购买和合买所花费的时间,让你计算出k个人总共花费的时间,然后再稍作处理就可得到答案,具体格式看题意。
本题思路:简单dp,用dp[ i ]来存储前i个人购买票所需要的最小时间,则很容易得出状态转移方程为dp[ i ] = min (dp[i - 1] + a[ i ], dp[i - 2] + b[i - 1]);
参考代码:
1 #include <iostream> 2 #include <cstring> 3 #include <algorithm> 4 #include <cstdio> 5 using namespace std; 6 7 const int maxk = 2e3 + 5; 8 int k, a[maxk], b[maxk], dp[maxk]; 9 10 int main () { 11 int n; 12 cin >> n; 13 while(n --) { 14 memset(vis, false, sizeof vis); 15 cin >> k; 16 for(int i = 1; i <= k; i ++) 17 cin >> a[i]; 18 for(int i = 1; i < k; i ++) 19 cin >> b[i]; 20 dp[1] = a[1]; 21 for(int i = 2; i <= k; i ++) { 22 dp[i] = min(dp[i - 1] + a[i], dp[i - 2] + b[i - 1]); 23 } 24 int h = dp[k] / 3600; 25 int hour = 8 + h; 26 h = dp[k] % 3600; 27 int minute = h / 60; 28 h = h % 60; 29 int s = h; 30 char ss[3] = "am"; 31 if(hour > 12) { 32 hour -= 12; 33 strcpy(ss, "pm"); 34 } 35 printf("%02d:%02d:%02d %s\n", hour, minute, s, ss); 36 } 37 return 0; 38 }View Code
标签:Tickets,02d,HDU,12,int,1260,maxk,include,dp 来源: https://www.cnblogs.com/bianjunting/p/10648228.html