Tickets hdu 1260(dp)

http://acm.hdu.edu.cn/showproblem.php?pid=1260

题意:现有N个人在买票,给出两种购买票的时间,一是每个人都排队买票(N),另一个是两个两个相邻的人买票的时间的总和(N-1),现在从早上八点开始买票,问你这N个人总共买完票所需的最短时间是多少?

分析:要想求最短时间,那么肯定要比较第一种方法相邻两个人加起来的时间和与第二种方法直接给你的时间和进行比较,那么现在就有一个动态方程:

     dp[i]=min(dp[i-1]+a[i],dp[i-2]+a[i])(i>=2,dp[i]表示前i个人购买票所需要的最小时间和)

#include<stdio.h>
#include<math.h>
#include<string.h>
#include<ctype.h>
#include<stdlib.h>
#include <iostream>
#include<algorithm>
#include<queue>
#define maxn 2100
#define oo 0x3f3f3f3f
using namespace std;
int a[maxn], b[maxn], dp[maxn];

int main()
{
    int T, n;

    scanf("%d", &T);

    while(T--)
    {
        scanf("%d", &n);

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

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

        if(n == 1)
        {
            printf("08:00:%02d am
", a[1]);
            continue;
        }

        memset(dp, 0, sizeof(dp));
        dp[0]=0;
        dp[1]=a[1];

        for(int i=2; i<=n; i++)
         dp[i]=min(dp[i-1]+a[i], dp[i-2]+b[i]);

         int k = dp[n];

         int h = (k/3600+8)%24;
         int m = k%3600/60;
         int s = k%3600%60;

         if(h<12) printf("%02d:%02d:%02d am
", h, m, s);
         else printf("%02d:%02d:%02d pm
", h, m, s);

    }
    return 0;
}
View Code
原文地址:https://www.cnblogs.com/daydayupacm/p/5737575.html