The formula is very simple, that is, the initial value should be handled well.
#include <stdio.h> #include <string.h> #include <algorithm>using namespace std;const int inf=9999999; int S[2005],d[2005],dp[2005][2];//dp[i][0] The number of the first I is processed independently, the maximum value of the number of previous I dp[i][1] The number of first I and the preceding number, the maximum value of the previous I number int main () {#ifndef Online_judgefreopen ("In.txt", "R", stdin), #endifint t,n;scanf ("%d", &t), while (t--) {scanf ("%d", &n); for (int i =1;i<=n;i++) {Dp[i][0]=inf;dp[i][1]=inf;} for (int i=1;i<=n;i++) scanf ("%d", &s[i]), and for (int i=2;i<=n;i++) scanf ("%d", &d[i]);DP [1][0]=s[1]; DP[1][1]=S[1];DP [0][0]=0;dp[0][1]=0;for (int i=2;i<=n;i++) {dp[i][0]=min (dp[i-1][1],dp[i-1][0]) +s[i];DP [i][1]= Min (dp[i-2][0],dp[i-2][1]) +d[i];} int ans=min (dp[n][0],dp[n][1]); int H,m,s;h=ans/3600;ans=ans%3600;m=ans/60;ans=ans%60;s=ans;int flag=0;if (8+h>= {flag=1;h-=12;} if (8+h<10) printf ("0%d:", 8+h), Else printf ("%d:", 8+h), if (m<10) printf ("0");p rintf ("%d:", m); if (s<10) printf ("0"), if (flag==0) printf ("%d am\n", s), Else printf ("%d pm\n", s);}}
HDU 1260 Tickets