令dp[i]为前i个人买好票的最少花费。
那么这个状态可由2种状态转移而来:1.上一个人和自己组队买票 2.上一个人不和自己组队买票
即可得:dp[i] = min(dp[i-1]+cost[i], dp[i-2]+together[i]);
#include <iostream> #include <bits/stdc++.h> using namespace std; #define ll long long #define mst(a,b) memset(a,b,sizeof(a)) #define rep(i,a,b) for(ll i=(a);i<(b);++i) #define scf(n) scanf("%d", &(n)); const double eps = 1e-8, PI = acos(-1.0f); const int inf = 0x3f3f3f3f, maxN = 1e5 + 5; const int maxT = 2005; int N, M, T; int ti[maxT]; int yq[maxT]; // dp[i]即前i个人买票最少花费 int dp[maxT]; int main() { int K; scf(T); while (T--) { scanf("%d", &K); rep(i, 1, K + 1) scf(ti[i]); rep(i, 2, K + 1) scf(yq[i]); dp[0] = 0; for (int i = 1; i <= K; ++i) { int d1, d2 = inf; d1 = dp[i - 1] + ti[i]; if (i - 2 >= 0) d2 = dp[i - 2] + yq[i]; dp[i] = min(d1, d2); } int hh, mm, ss = dp[K]; hh = ss / 3600; ss -= hh * 3600; mm = ss / 60; ss -= mm * 60; hh += 8; char fg[10]; if (hh >= 12) strcpy(fg, "pm"); else strcpy(fg, "am"); printf("%02d:%02d:%02d %s\n", hh, mm, ss, fg); } return 0; }