1260 Tickets【dp】

来源:互联网 发布:js字符串是一个数组 编辑:程序博客网 时间:2024/05/16 02:31

Tickets

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


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

第一次见到这个题是在昨天的一次联合外校的联谊赛中,绞尽脑汁,才在比赛开始三小时之后提交.............之前没一点dp 的基础,如果说有的话,那就是lcs了...然后现在又做了一遍,结果一马虎,贡献了一个wa,呃呃呃.......下次要注意!比赛可不是闹着玩的....


动态规划,主要是找状态转移方程,把某个题的动态求最优解过程弄清楚,这道题也就没太大问题了,这道题的思路是:类似于lcs的情况(所以当时在赛场上直接按模式套用了....),不过只是一维的,比较简单,就是每一次都选择两者一起和单独耗费的时间最少的那一种,每次记录局部最优解,最后得到全局最优解......


#include<stdio.h>#include<string.h>#define min(a,b) (a<b?a:b)int a[2005],b[2005],dp[2005],sum,n;void ticket()//计算时间{memset(dp,0,sizeof(dp));dp[1]=a[1];sum=0;//初始化...for(int i=2;i<=n;++i){dp[i]=min(dp[i-1]+a[i],dp[i-2]+b[i]);//动态转移方程}sum=dp[n];}void time()//处理输出{int h=8,m=0,s,kase=0;h+=sum/3600;sum%=3600;m+=sum/60;sum%=60;s=sum;//这里时间处理也是醉了....if(h>12)//下午了{kase=1;h%=12;}printf("%02d:%02d:%02d ",h,m,s);if(!kase)//输出不同{printf("am\n");return;}printf("pm\n");}int main(){    int t;    scanf("%d",&t);    while(t--)    {    int i;        scanf("%d",&n);        for(int i=1;i<=n;++i)        {        scanf("%d",a+i);}for(i=2;i<=n;++i){scanf("%d",b+i);}ticket();//调用函数time();    }    return 0;}






0 0
原创粉丝点击