HDU 1260 Tickets

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1260


题意:有n个人排队买票。给出n个人单独的买票时间s,已经一个人与在他前面的另一个人一同票花的时间sc,问最少花费多少时间


思路:很容易就想到转移方程是dp[i]=max(dp[i-1]+s[i],dp[i-2]+sc[i]),最后转换成XX:XX:XX的形式记得补上0就可以了



#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

int s[2030],sc[2030],dp[2030];

int main()
{
    int t,n;
    scanf("%d",&t);
    while (t--)
    {
        memset(dp,0,sizeof(dp));
        scanf("%d",&n);
        for (int i=1;i<=n;i++)
          scanf("%d",&s[i]);

        for (int i=2;i<=n;i++)
          scanf("%d",&sc[i]);

        dp[0]=0;
        dp[1]=s[1];
        for (int i=2;i<=n;i++)
        {
            dp[i]=min(dp[i-1]+s[i],dp[i-2]+sc[i]);
        }

        int hour=dp[n];
        int ss=hour%60;
        hour/=60;
        int min=hour%60;
        hour/=60;
        int jd=(hour+8)/12;
        hour=(hour+8)%12;
        if (jd%2==0) printf("%02d:%02d:%02d am\n",hour,min,ss);
        else printf("%02d:%02d:%02d pm\n",hour,min,ss);
    }
}


你可能感兴趣的:(dp,ACM,HDU)