http://acm.hdu.edu.cn/showproblem.php?pid=1260
The main topic: n Individuals buy tickets, everyone takes time to buy tickets, the adjacent two people can buy tickets together to save time;
So a person can buy their own tickets can also be bought with the front of the people can also buy with the people behind, and the back of the people to buy it is equivalent to the back of the people in front of the people to buy together;
Therefore, a person has two ways to buy tickets themselves or with the people in front to buy, choose the shortest time;
Get the state equation of DP:
-dp[i] = min (dp[i-1] + a[i], dp[i-2] + b[i]);
Examples:
1
5
15 5 10) 5 20
20 10 7 10
1 2 3 4 5 each person's number
(1) The time that I personally buy tickets for everyone
(2) represents the first person and the front one to buy tickets together public time
(3) The time it takes for everyone to buy their own tickets
(4) Indicates the time spent buying tickets with every two people
Each DP goes (1), the minimum value (2)
#include <stdio.h>#include<string.h>#include<math.h>#include<algorithm>#include<stdlib.h>#defineN 2010using namespacestd;intMain () {intI, T, N, A[n], b[n], dp[n], m; scanf ("%d", &t); while(t--) { intH, M, S; scanf ("%d", &N); for(i =1; I <= N; i++) scanf ("%d", &A[i]); for(i =2; I <= N; i++) scanf ("%d", &B[i]); dp[1] = a[1]; for(i =2; I <= N; i++) Dp[i]= Min (Dp[i-1] + a[i], dp[i-2] +B[i]); M=Dp[n]; H= m/3600; M= (M-h *3600) / -; S= M-h *3600M -; H+=8; if(H <= A) printf ("%02d:%02d:%02d am\n", H, M, S); Elseprintf ("%02d:%02d:%02d pm\n", H, M, S); } return 0;}
HDU 1260 Tickets