您的位置:首页 > 其它

HDU 1260------简单DP

2015-07-16 21:45 288 查看
Problem Description

Jesus, what a great movie! Thousands of people are rushing to the cinema. However, this is really a tuff time for Joe who sells the film tickets. He is wandering when could he go back home as early as possible.

A good approach, reducing the total time of tickets selling, is let adjacent people buy tickets together. As the restriction of the Ticket Seller Machine, Joe can sell a single ticket or two adjacent tickets at a time.

Since you are the great JESUS, you know exactly how much time needed for every person to buy a single ticket or two tickets for him/her. Could you so kind to tell poor Joe at what time could he go back home as early as possible? If so, I guess Joe would full
of appreciation for your help.

Input

There are N(1<=N<=10) different scenarios, each scenario consists of 3 lines:

1) An integer K(1<=K<=2000) representing the total number of people;

2) K integer numbers(0s<=Si<=25s) representing the time consumed to buy a ticket for each person;

3) (K-1) integer numbers(0s<=Di<=50s) representing the time needed for two adjacent people to buy two tickets together.

Output

For every scenario, please tell Joe at what time could he go back home as early as possible. Every day Joe started his work at 08:00:00 am. The format of time is HH:MM:SS am|pm.

Sample Input

2
2
20 25
40
1
8


Sample Output

08:00:40 am
08:00:08 am


Source

浙江工业大学第四届大学生程序设计竞赛

题意:
JOE是个卖票的人,他想要在最短的时间内把票卖完,他在上午八点开始卖票,求回到家的时间。
有N个样例,K表示有K个人,下面一行为每个人买票所需要的时间a,在下一行有K-1个数据,表示相邻两个人一起买票所需要的时间b。
dp[j]表示从第一个人到j人买票所花的最短时间,由题可得决策就两个过程,是选a还是选b。DP方程如下:

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

下面答案的输出有点繁琐,由于是am,pm表示,所以是12小时制。在24小时制的情况下0:00:00~12:59:59都是am,剩下的就是pm。代码如下:
#include<stdio.h>
#include<string.h>
#define min(a,b) a<b?a:b
int dp[2001];
int main()
{
int n,sum,h,m,s,t,k;
scanf("%d",&n);
while(n--)
{
scanf("%d",&k);
int a[2001],b[2001];
for(int i=1;i<=k;i++)
{
scanf("%d",&a[i]);
}
dp[1]=a[1];
dp[0]=0;
for(int i=1;i<k;i++)
scanf("%d",&b[i]);
for(int i=2;i<=k;i++)
dp[i]=min(dp[i-1]+a[i],dp[i-2]+b[i-1]);
sum=dp[k];
h=sum/3600;
sum=sum%3600;
m=sum/60;
s=sum%60;
h=(h+8)%24;
t=h/13;
h=h%12;
if(h>=10)
printf("%d:",h);
else
printf("0%d:",h);
if(m>=10)
printf("%d:",m);
else
printf("0%d:",m);
if(s>=10)
printf("%d ",s);
else
printf("0%d ",s);
if(t)
printf("pm\n");
else
printf("am\n");
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: