HDU 1260 Tickets DP

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

用dp[i]表示处理到第i个的时候用时最短。

那么每一个新的i,有两个选择,第一个就是自己不和前面的组队,第二就是和前面的组队。

那么dp[i] = min(dp[i - 1] + a[i], dp[i - 2] + b[i]);  前者是自己组队。

边界条件 dp[0] = 0; dp[1] = a[1];

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL;

#include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>

const int maxn = 2000 + 20;
int a[maxn];
int b[maxn];
int dp[maxn];
void work() {
    int n;
    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]);
    }
    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]);
    }
//    cout << dp[n] << endl;
    int addHour = dp[n] / 60 / 60;
    int addMin = (dp[n] / 60) % 60;
    int addSec = dp[n] % 60;
    printf("%02d:%02d:%02d ", 8 + addHour, addMin, addSec);
    if (8 + addHour > 12) {
        printf("pm
");
    } else if (8 + addHour == 12) {
        if (addMin || addSec) {
            printf("am
");
        } else printf("pm
");
    }  else printf("am
");
    return;
}
int main() {
#ifdef local
    freopen("data.txt","r",stdin);
#endif
    int t;
    scanf("%d", &t);
    while (t--) work();
    return 0;
}
View Code
原文地址:https://www.cnblogs.com/liuweimingcprogram/p/6003948.html