Question link: http://acm.hdu.edu.cn/showproblem.php? Pid = 1, 1176
Train of thought; pre-process the number of biscuits at each position at each time point, and then perform DP by time. For details, refer to the code, and the implementation is very simple.
# Include <iostream> # include <stdio. h> # include <stdlib. h> # include <string. h >#include <algorithm> # define MAXN 100010 # define MAXM 15 using namespace std; int f [MAXN] [MAXM]; // f [I] [j] = time I stands in the position of the int num [MAXN] [MAXM] in the j bag; // num [I] [j] = the number of biscuits in position j when time I is used int max (int a, int B) {if (a> B) return; return B;} int main () {int n; while (scanf ("% d", & n )! = EOF & n) {memset (f, 0, sizeof (f); memset (num, 0, sizeof (num); int lastTime = 0; // lastTime = the time when the last biscuit falls for (int I = 1; I <= n; I ++) {int x, T; scanf ("% d", & x, & T); num [T] [x] ++; lastTime = max (lastTime, T );} for (int I = lastTime; I> = 0; I --) for (int j = 0; j <= 10; j ++) {f [I] [j] = f [I + 1] [j] + num [I] [j]; if (J-1> = 0) f [I] [j] = max (f [I] [j], f [I + 1] [j-1] + num [I] [j]); if (j + 1 <= 10) f [I] [j] = max (f [I] [j], f [I + 1] [j + 1] + num [I] [j]);} int ans = 0; for (int I = 0; I <= 10; I ++) ans = max (ans, f [0] [5]); printf ("% d \ n", ans);} return 0 ;}
[HDU 1176] free pie (DP)