【动态规划】 HDU1260 - Tickets

Tickets

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 923    Accepted Submission(s): 467


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

 

简单一维动规题。大概意思是k个人买票,可以一个人买,也可以相邻的两个人一起买。现在给出每个人单独买票的时间和每两个相邻的人买票的时间,求最少买票时间。

思路:

用one[i]表示第i个人单独买票的时间,two[i]表示第i个人和他前面的人(第i-1个人)一起买票的时间。

关键就是写出状态方程:dp[i] = min(dp[i-1] + one[i], dp[i-2] + two[i])

下面给出AC代码,有参考

复制代码
 1 #include <iostream>
 2 #include <cstring>
 3 #include <algorithm>
 4 using namespace std;
 5 
 6 
 7 int main(){
 8     int n,k;
 9     int one[2001],two[2001],dp[2001];
10     cin>>n;  //有几组
11     while(n--){
12         memset(one,0,sizeof(one));  //每次开始新的一轮时初始化
13         memset(two,0,sizeof(two));
14         memset(dp,0,sizeof(dp));
15         cin>>k;  //总人数
16         for(int i=1;i<=k;i++)
17             cin>>one[i];
18         for(int i=2;i<=k;i++)  //如果只有一个人买票,则不用输入two[i],此处巧妙!
19             cin>>two[i];
20         dp[0] = 0,dp[1] = one[1];   //给dp[0]赋值0,则可直接从dp[2]开始循环,此处巧妙!
21         for(int i=2;i<=k;++i)
22             dp[i] = min(dp[i-1]+one[i],dp[i-2]+two[i]);
23         int h = dp[k]/3600 +8;
24         int m = dp[k]/60 % 60;
25         int s = dp[k]%60;
26         if(h<12)
27             printf("%02d:%02d:%02d am\n",h,m,s);
28         else
29             printf("%02d:%02d:%02d pm\n",h,m,s);
30     }
31 
32     return 0;
33 }
复制代码

 

posted @   Aikoin  阅读(185)  评论(0编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
点击右上角即可分享
微信分享提示