题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1260
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.
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.
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
解题思路:这是一道简单的DP,题目的意思就是有两种购票方式,要么采用单独购票,要么采用双人购票,求N个人购完票所花费的最小时间。简单推导易得状态转移方程:dp[i] = min(dp[i-1]+tim[i],dp[i-2]+together[i]);两种情况:①前面i-1个人所消耗的时间加上当前单人购票时间;②前i-2个人购票时间加上当前双人购票时间;取这两种情况的最小值即为最小花费时间。之后还要对时间显示格式进行处理,这里应该是12小时制,即超过12小时显示为pm且取余12。
AC代码:
#include<bits/stdc++.h>
using namespace std;
int tim[],together[],dp[];
int main(){
int N,K;
cin>>N;
while(N--){//表示N种情况
cin>>K;//表示总人数
for(int i=;i<=K;i++)cin>>tim[i];//读入单个人购票消耗时间
for(int i=;i<=K;i++)cin>>together[i];//读入连续两人购票所消耗的时间
dp[]=,dp[]=tim[];//初始两个人的购票时间为dp[0]=0,第一个人单人购票所花费的时间dp[1]=tim[1]
for(int i=;i<=K;i++)//从第二个人购票开始计算时间
dp[i]=min(dp[i-]+tim[i],dp[i-]+together[i]);
int sec=dp[K]%;//保存秒
int minu=(dp[K]/)%;//保存分钟
int hour=dp[K]/+;//保存小时
int flag=;//标记是否超过12点
if(hour>){flag=;hour%=;}
printf("%02d:%02d:%02d ",hour,minu,sec);
if(flag)cout<<"pm"<<endl;
else cout<<"am"<<endl;
}
return ;
}