HDOJ 题目1260 Tickets(动态规划)

来源:互联网 发布:hifi耳机 知乎 编辑:程序博客网 时间:2024/04/29 03:49

Tickets

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


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
2220 254018
 

Sample Output
08:00:40 am08:00:08 am
 

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

Recommend
JGShining   |   We have carefully selected several similar problems for you:  1160 1231 1074 1159 1264 
 

题意:有k个人要买票(你也在里面最后一位),每个人买票要花时间,或者两个相邻的人一起买会有一个时间, 问你最少花费多少时间才能买到票


思路:题意看的半懂的,结合sample差不多理解了,

以某个人节点,他要么单独买,要么和前面一个人或者后面一个人一起买,

和后面一个人一起买其实可以看成是后面一个人和他前面的一个人一起买,

因此相当于只有两种情况,要么自己一个人买,要么和前面一个人一起买

于是设计dp[i][0],dp[i][1]

0:代表自己一个人买时到他这个位置所需要的最少时间

1:代表自己和前面一个人一起买到他当前位置所需的最少时间


于是状态转移方程就出来了

dp[i][0] = max(dp[i-1][0], dp[i-1][1]) + t[i]; (i >= 1 && i <= n, t表示单独买所需的时间)

dp[i][1] = max(dp[i-2][0], dp[i-2][1]) + interval[i]; (i >= 2 && i <=n, interval表示和前面一个人一起买所需的时间)


最后的answer就是 max(dp[n][0], dp[n][1])了

这个结果是s,换算一下变成时分秒 然后加上早上8点整 大于12点就对12取模,下午是pm,上午是am

ac代码
#include<stdio.h>#include<string.h>int dp[2020][2],t1[2020],t2[2020];int min(int a,int b){if(a>b)return b;elsereturn a;}int main(){int t;scanf("%d",&t);while(t--){int n,i,ans,h,m,s;scanf("%d",&n);for(i=1;i<=n;i++){scanf("%d",&t1[i]);}for(i=2;i<=n;i++){scanf("%d",&t2[i]);}memset(dp,0,sizeof(dp));t2[1]=t1[1];for(i=1;i<=n;i++){dp[i][0]=min(dp[i-1][0],dp[i-1][1])+t1[i];if(i==1)dp[i][1]=t1[i];elsedp[i][1]=min(dp[i-2][0],dp[i-2][1])+t2[i];}ans=min(dp[n][0],dp[n][1]);h=8+ans/3600;ans%=3600;m=ans/60;ans%=60;s=ans;if(h>12){printf("%02d:%02d:%02d pm\n",h,m,s);}elseprintf("%02d:%02d:%02d am\n",h,m,s);}}


0 0
原创粉丝点击