题意:n个人来买票,a[i]表示第i个人买票要多久,b[i]表示第i个人和第i+1个人一起买票需要多久。
问:最少多长时间卖完
状态转移方程dp[i] = min(dp[i-1]+a[i],dp[i-2]+b[i-1]);
#include <iostream> #include <cmath> #include <cstdio> #include <cstring> #include <string> #include <map> #include <iomanip> #include <algorithm> #include <queue> #include <stack> #include <set> #include <vector> //const int maxn = 1e5+5; #define ll long long ll gcd(ll a,ll b){return b?gcd(b,a%b):a;} ll lcm(ll a,ll b){return a/gcd(a,b)*b;} #define MAX INT_MAX #define FOR(i,a,b) for( int i = a;i <= b;++i) #define bug cout<<"--------------"<<endl using namespace std; int dp[2100],a[2100],b[2100]; int main() { int T; scanf("%d",&T); while(T--) { memset(dp,0,sizeof(dp)); int n; scanf("%d",&n); FOR(i,1,n) scanf("%d",a+i); FOR(i,1,n-1) scanf("%d",b+i); dp[1] = a[1]; for(int i=2;i<=n;++i) { for(int j=1;j<i;++j) { dp[i] = min(dp[i-1]+a[i],dp[i-2]+b[i-1]); } } int h=dp[n]/3600; int m=dp[n]%3600/60; int s=dp[n]%60; printf("%02d:%02d:%02d ",(h+8)%24,m,s); if((h+8)%24>12) printf("pm "); else printf("am "); } }