kuangbin专题十二 HDU1260 Tickets (dp)
Tickets
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 8077 Accepted Submission(s): 4101
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
Sample Output
08:00:40 am 08:00:08 am
题目大意:给出n个人,n个数表示每人单买的价格,n-1个数,表示相邻的两个人合买的价格。
思路:画了画,感觉挺难的。然后深搜写了,感觉得记忆化,然后发现可以用dp[pos], 表示到达该点之前的所有和的最小值。(具体见注释)
dp递推的状态方程没有写出来。
dp[i] = minn(dp[i-1] + a[i], dp[i-2] + b[i-1]) // 单人,双人
1 #include <iostream> 2 #include <stdio.h> 3 #include <math.h> 4 #include <string.h> 5 #include <stdlib.h> 6 #include <string> 7 #include <vector> 8 #include <set> 9 #include <map> 10 #include <queue> 11 #include <algorithm> 12 #include <sstream> 13 #include <stack> 14 using namespace std; 15 #define mem(a,b) memset((a),(b),sizeof(a)) 16 #define mp make_pair 17 #define pb push_back 18 #define fi first 19 #define se second 20 #define sz(x) (int)x.size() 21 #define all(x) x.begin(),x.end() 22 typedef long long ll; 23 const int inf = 0x3f3f3f3f; 24 const ll INF =0x3f3f3f3f3f3f3f3f; 25 const double pi = acos(-1.0); 26 const double eps = 1e-5; 27 const ll mod = 1e9+7; 28 //head 29 int n, a[2010], b[2010], minn, dp[2010]; 30 void dfs(int pos, int sum) { 31 if(pos >= n) {//如果pos >= n 32 if(sum < minn) 33 minn = sum;//更新minn 34 return; 35 } 36 if(sum >= dp[pos]) //如果达到pos的sum 比之前到达的还大,没有必要再搜索了 37 return; 38 dp[pos] = sum;//更新dp[pos] 39 dfs(pos+1, sum + a[pos]); 40 dfs(pos+2, sum + b[pos]); 41 } 42 43 int main() { 44 int t; 45 while(~scanf("%d", &t)) { 46 while(t--) { 47 scanf("%d", &n); 48 minn = inf; 49 for(int i = 0; i < n; i++) 50 scanf("%d", &a[i]); 51 for(int i = 0; i < n-1; i++) 52 scanf("%d", &b[i]); 53 b[n-1] = a[n-1];//手动添加一个 54 fill(dp, dp + n, inf);//初始化inf 55 dfs(0, 0);//dfs(pos, sum) 56 int h, m, s;//时间处理 57 h = minn / 3600; 58 m = (minn - h * 3600) / 60; 59 s = minn % 60; 60 printf("%02d:%02d:%02d ", 8 + h, m, s); 61 if(h < 12) 62 printf("am\n"); 63 else if(h == 12 && (m + s) == 0) 64 printf("am\n"); 65 else 66 printf("pm\n"); 67 } 68 } 69 }