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

 #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 ;
}

最新文章

  1. 第二篇:Entity Framework CodeFirst &amp; Model 映射
  2. Scorpio-CSharp简介
  3. caffe pytho接口
  4. xshell5 启动显示 mfc110.dll msvcp110.dll 未找到问题 解决办法
  5. VC++检测当前网络状态
  6. esriSRGeoCS3Type Constants
  7. SQL技术内幕-12 SQL优化方法论前言
  8. 认识和理解css布局中的BFC
  9. POJ 3181 Dollar Dayz 简单DP
  10. 用 BeautifulSoup爬取58商品信息
  11. 学习目标或者作业的制定(SMART原则)
  12. 文件系统常用命令df、du、fsck、dumpe2fs
  13. 通过Xshell如何从Linux服务器下载文件(亲测可行)
  14. dom渲染方面的优化浅谈
  15. PHP保留两位小数并且四舍五入及不四舍五入的方法
  16. Fiddler使用
  17. 微信小程序记账本进度二
  18. SpringMVC 请求响应流程
  19. Docker环境的持续部署优化实践
  20. 浅谈 iOS 中的 Activity Indicator

热门文章

  1. Android之键盘监听的执行机理【看清键盘监听的本质】【入门版】
  2. Android Studio keymap到Eclipse后,查找下一个同样变量快捷键Ctrl+K失效
  3. Elasticsearch - 搜索类型与搜索位置
  4. 两个喜欢的&quot;新&quot;C#语法
  5. Class.forName() 详解
  6. hexSHA1散列加密解密(不可逆)
  7. HDU3642 Get The Treasury —— 求矩形交体积 线段树 + 扫描线 + 离散化
  8. 并不对劲的DFT
  9. SPOJ:NT Games(欧拉函数)
  10. 【HDU 2167】 Pebbles