HDU-1260 Tickets(dp)

叁歲伎倆 2022-04-25 05:10 168阅读 0赞

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:

  1. 2
  2. 2
  3. 20 25
  4. 40
  5. 1
  6. 8

Sample Output:

  1. 08:00:40 am
  2. 08:00:08 am

题目大意:

Joe卖电影票,他可以一次性卖一张票也可以连着卖两张票,卖票需要花时间,Joe从早8点开始卖票,Joe想早卖完票早回家,问Joe最早卖完票是在什么时候。

先输入样例的个数N表示一共需要处理N个样例,每个样例的第一行表示需要买票的总人数K,第二行输入的是一个数组表示的是卖单张票给一个人所要花的时间,第三行输入的也是一个数组表示卖两张连着的票给两个人所要花的时间。

思路:

Joe每次卖票都有两种选择,卖单张或者连着卖两张,但是要保证花的总时间最短,是一个dp(动态规划)问题。卖票卖到一个人的时候,dp方程为Min(dp[i-1]+a[i],dp[i-2]+b[i]),其中a[i]为卖单张票给第i+1个人所要花的时间,b[i]为卖两张连着的票给第i个人和第i+1个人所要花的时间。最后所要花的最短时间就是dp[K-1],即完成第K个人的卖票所要花的最短时间。最后不要忘记处理时间的输出格式。

上AC代码:

  1. #include <stdio.h>
  2. int n,k;
  3. int dp[2000];
  4. int a[2000];
  5. int b[2000];
  6. int Min(int a,int b)
  7. {
  8. return a<b?a:b;
  9. }
  10. int main()
  11. {
  12. scanf("%d",&n);
  13. while(n--)
  14. {
  15. int i;
  16. scanf("%d",&k);
  17. for(i=0;i<k;i++)
  18. {
  19. scanf("%d",&a[i]);
  20. }
  21. for(i=1;i<=k-1;i++)
  22. {
  23. scanf("%d",&b[i]);
  24. }
  25. dp[0]=a[0];
  26. dp[1]=Min(dp[0]+a[1],b[1]);
  27. for(i=2;i<k;i++)
  28. {
  29. dp[i]=Min(dp[i-1]+a[i],dp[i-2]+b[i]);
  30. }
  31. int dert=dp[k-1];
  32. int h=8,m=0,s=0;
  33. bool am=true;
  34. while(dert-60>0)
  35. {
  36. m++;
  37. if(m>=60)
  38. {
  39. m=0;
  40. h++;
  41. if(h>=12)
  42. {
  43. am=false;
  44. h=0;
  45. }
  46. }
  47. dert-=60;
  48. }
  49. s+=dert;
  50. printf("%02d:%02d:%02d ",h,m,s);
  51. if(am)
  52. printf("am\n");
  53. else
  54. printf("pm\n");
  55. }
  56. return 0;
  57. }

发表评论

表情:
评论列表 (有 0 条评论,168人围观)

还没有评论,来说两句吧...

相关阅读