HDU 1260 —— Tickets

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

题意:有n个人要买票,可以单个人买,也可以和前面一个人一起买,给出每个人买票的时间(n个数)以及和前一个人一起买票的时间(n-1个数);问最少买票时间;

dp[i]表示i个人买票最少时间;

状态转移:dp[i] = min(dp[i-1]+time[i], dp[i-2]+w[i]);

dp[1] = time[1];

注意输出格式;


#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn = 2100;
int n, k;
int dp[maxn], time[maxn], w[maxn];

int main()
{
	scanf("%d", &n);
	while(n--)
	{
		scanf("%d", &k);
		for(int i = 1;i<=k;i++)
			scanf("%d", &time[i]);
		for(int i = 2;i<=k;i++)
			scanf("%d", &w[i]);
		dp[1] = time[1];
		for(int i = 2;i<=k;i++)
		dp[i] = min(dp[i-1]+time[i], dp[i-2]+w[i]);
		int t = dp[k];
		int s = t % 60;
		int m = t % 3600 / 60;
		int h = t / 3600;
		h = (8+h) % 24;
		printf("%02d:%02d:%02d ", h, m, s);
		if(h > 12)	printf("pm\n");
		else	printf("am\n");
	}
	return 0;
}


你可能感兴趣的:(HDU 1260 —— Tickets)