HDU 1069 Monkey and Banana, hdu1069

I feel that the pure dp question is basically to calculate a maximum value for some numbers or objects.

There is a restricted relationship between the above layer and the placement of the next layer, so we need to add a flag to mark which is the original top layer, and then dp.

A certain type of Square is infinitely available, that is, it is equivalent to each type of square. In fact, there are three kinds of square, and each type of square can only be put one

Sort coordinates by x

Dp [I] [j] indicates the maximum height of the stacked I layer when the top layer is placed in the j Block.

Dp [I] [j] = max (dp [I] [j], dp [I-1] [0, 1, 2 .... l] + z [j])

Then add a judgment condition.

if((r[k].x>r[j].x&&r[k].y>r[j].y)||(r[k].y>r[j].x&&r[k].x>r[j].y))

At the beginning, I wrote

if(r[k].x>r[j].x&&r[k].y>r[j].y)

Always in WA

# Include <stdio. h> # include <algorithm> # include <string. h> using namespace std; const int inf = 1 <30; # define N 105 struct Rec {int x, y, z;} r [N]; int cmp (Rec a, Rec B) {if (. x = B. x) return. y> B. y; return. x> B. x;} int dp [N] [N]; // dp [I] [j]: In the front I block, maximum height void init () {memset (dp, 0, sizeof (dp) When block j is placed on the top layer;} int main () {# ifndef ONLINE_JUDGEfreopen ("in.txt", "r", stdin); # endifint n, cas = 1; while (scanf ("% d", & n), n) {init (); int l = 1, xx, yy, zz; for (int I = 0; I <n; I ++) {scanf ("% d", & xx, & yy, & zz); r [l]. x = xx; r [l]. y = yy; r [l ++]. z = zz; // r [l]. x = xx; r [l]. y = zz; r [l ++]. z = yy; r [l]. x = yy; r [l]. y = zz; r [l ++]. z = xx; // r [l]. x = yy; r [l]. y = xx; r [l ++]. z = zz; r [l]. x = zz; r [l]. y = xx; r [l ++]. z = yy; // r [l]. x = zz; r [l]. y = yy; r [l ++]. z = xx;} r [0]. x = inf; r [0]. y = inf; r [0]. z = inf; sort (r + 1, r + l, cmp); for (int I = 1; I <l; I ++) {// I is the layer of the tower. for (int j = I; j <l; j ++) {// j <I is meaningless, because j contains the maximum number of J-1, And I represents the number of layers for (int k = 0; k <l; k ++) {if (r [k]. x> r [j]. x & r [k]. y> r [j]. y) | (r [k]. y> r [j]. x & r [k]. x> r [j]. y) dp [I] [j] = max (dp [I] [j], dp [I-1] [k] + r [j]. z) ;}} int ans = 0; for (int I = 1; I <l; I ++) {for (int j = I; j <l; j ++) {ans = max (ans, dp [I] [j]) ;}} printf ("Case % d: maximum height = % d \ n ", cas ++, ans );}}