13861. Pogo-cow Constraints
Time limit:1 secs, Memory limit:256 MB
Description
In an ill-conceived attempt to enhance the mobility of his prize cow Bessie, Farmer John have attached a pogo stick to each Of Bessie ' s legs. Bessie can now hop around quickly throughout the farm, but she had not yet learned what to slow down.
to help train Bessie to hop with greater control, Farmer John sets up a practice course for her along A straight one-dimensional path across his farm. At various distinct positions in the path, he places n targets on which Bessie should try-to-land (1 <= N <= 1000). Target I was located at position x (i), and was worth P (i) points if Bessie lands on it. Bessie starts at the location of all target of her choosing and are allowed to move in only one direction, hopping from tar Get to target. Each hop must cover at least as much distance as the previous hop, and must land on a target.
Bessie receives credits for every target she touches (including the initial target on which she starts). Please compute the maximum number of points she can obtain.
Input
* Line 1:the integer N.
* Lines 2..1+n:line i+1 contains x (i) and P (i), each a integer in the range 0..1,000,000.
Output
* Line 1:the Maximum number of points Bessie can receive.
Sample Input
65 61 110 57 64 88 10
Sample Output
25
Hint
In the sample, there is 6 targets. The first is at position x=5 and are worth 6 points, and so on. Bessie hops from position x=4 (8 points) to position x=5 (6 points) to position x=7 (6 points) to position x=10 (5 points) .
Problem Source
2015 The second game of every Monday
First, all cows are sorted from left to right by position.
Consider the case of jumping from left to right, using f[i][j] to indicate that the cow is starting from the position of the first cow and jumping to
The location of the J-cow, the maximum score that can be obtained at the end.
Can get the dynamic programming state transfer equation: f[i][j] = P[i] + max{f[j][k]: The position K and the position J distance
Distance from J and position i}.
This has an O (n^2) state, and if the state transitions to enumerate all k greater than J, then the complexity is O (n^3).
But the problem has a monotonous nature. G[I][J] is the optimal decision (i.e. the optimal K value mentioned above), then there is
G[I][J-1]<=G[I][J]<=G[I+1][J].
Using monotonicity to reduce the enumeration range of K, the complexity can be reduced to O (n^2). The calculations for jumping from right to left are similar. Total
Complexity O (n^2).
#include <stdio.h> #include <string.h> #include <algorithm>using namespace std;struct cow {int x, p;} C[1005];int dp[1005][1005], n, ans = 0;bool cmp (const COW & C1, Const Cow & C2) {return c1.x < c2.x;} void DP (bool left2right) {int M;memset (DP, sizeof (DP), 0), for (int i = 0; i < n; i++) Dp[i][i] = c[i].p;for (int i = 0; I < n; i++) {for (int j = 0; J < i; J + +) {m = 0;if (left2right) {for (int k = j; k >= 0; k--) {if (c[j].x-c[k].x <= c[ i].x-c[j].x) {if (M < dp[j][k]) {m = Dp[j][k];}} else {break;}} Dp[i][j] = m + c[i].p;} else {for (int k = 0; k <= J; k++) {if (c[j].x-c[k].x >= c[i].x-c[j].x) {if (M < dp[j][k]) {m = Dp[j][k];}} else {break;}} if (M < dp[j][j]) m = dp[j][j];DP [i][j] = m + c[i].p;}}} for (int i = 0, i < n; i++) {for (int j = 0; J <= I; j + +) {if (ans < dp[i][j]) {ans = dp[i][j];}}} int main () {scanf ("%d", &n), for (int i = 0; i < n; i++) scanf ("%d%d", &c[i].x, &C[I].P); sort (c, C + N, CMP) ;DP(true);DP(false);p rintf ("%d\n", ans); return 0;}
Sicily 13861. Pogo-cow