题解报告:hdu 1260 Tickets

题目链接: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.

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

解题思路:这是一道简单的DP,题目的意思就是有两种购票方式,要么采用单独购票,要么采用双人购票,求N个人购完票所花费的最小时间。简单推导易得状态转移方程:dp[i] = min(dp[i-1]+tim[i],dp[i-2]+together[i]);两种情况:①前面i-1个人所消耗的时间加上当前单人购票时间;②前i-2个人购票时间加上当前双人购票时间;取这两种情况的最小值即为最小花费时间。之后还要对时间显示格式进行处理,这里应该是12小时制,即超过12小时显示为pm且取余12。

AC代码:

 1 #include
 2 using namespace std;
 3 int tim[2005],together[2005],dp[2005];
 4 int main(){
 5     int N,K;
 6     cin>>N;
 7     while(N--){//表示N种情况
 8         cin>>K;//表示总人数
 9         for(int i=1;i<=K;i++)cin>>tim[i];//读入单个人购票消耗时间
10         for(int i=2;i<=K;i++)cin>>together[i];//读入连续两人购票所消耗的时间
11         dp[0]=0,dp[1]=tim[1];//初始两个人的购票时间为dp[0]=0,第一个人单人购票所花费的时间dp[1]=tim[1]
12         for(int i=2;i<=K;i++)//从第二个人购票开始计算时间
13             dp[i]=min(dp[i-1]+tim[i],dp[i-2]+together[i]);
14         int sec=dp[K]%60;//保存秒
15         int minu=(dp[K]/60)%60;//保存分钟
16         int hour=dp[K]/3600+8;//保存小时
17         int flag=0;//标记是否超过12点
18         if(hour>12){flag=1;hour%=12;}
19         printf("%02d:%02d:%02d ",hour,minu,sec);
20         if(flag)cout<<"pm"<<endl;
21         else cout<<"am"<<endl;
22     }
23     return 0;
24 }

 

转载于:https://www.cnblogs.com/acgoto/p/8521784.html

你可能感兴趣的:(题解报告:hdu 1260 Tickets)