HDU 1260 Tickets(简单dp)

Tickets

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

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[0] = 0;

dp[1] = a[1];

dp[i] = min(dp[i-1] + a[i], dp[i-2] + b[i-1]);(i : 1->k)

解题代码:

 1 // File Name: Tickets 1260.cpp
2 // Author: sheng
3 // Created Time: 2013年05月24日 星期五 21时22分20秒
4
5 #include <iostream>
6 #include <stdio.h>
7 #include <string.h>
8 using namespace std;
9
10 const int max_k = 2003;
11
12 int dp[max_k], a[max_k], b[max_k];
13 int min (int a, int b)
14 {
15 return a > b ? b : a;
16 }
17
18 int main ()
19 {
20 int n;
21 int k, MIN;
22 int sec, minute, hour;
23 scanf ("%d", &n);
24 while (n --)
25 {
26 scanf ("%d", &k);
27 for (int i = 1; i <= k; i ++)
28 scanf ("%d", &a[i]);
29 for (int i = 1; i < k; i ++)
30 scanf ("%d", &b[i]);
31 dp[0] = 0;
32 dp[1] = a[1];
33 for (int i = 2; i <= k; i ++)
34 {
35 dp[i] = min(dp[i - 1] + a[i], dp[i - 2] + b[i - 1]);
36 }
37 // printf ("%d\n", dp[k]);
38 minute = dp[k]/60;
39 sec = dp[k]%60;
40 hour = 8 + minute/60;
41 minute %= 60;
42 if (hour > 12)
43 hour -= 12;
44 if (hour < 10)
45 printf ("0");
46 printf ("%d:", hour);
47 if(minute < 10)
48 printf ("0");
49 printf ("%d:", minute);
50 if (sec < 10)
51 printf ("0");
52 printf ("%d", sec);
53 printf (" %s\n", hour <= 12 ? "am" : "pm");
54 }
55 return 0;
56 }

G++

上一篇:spring-cloud-hystrix熔断


下一篇:安全扫描工具-AppScan