The topic for light OJ 1017.
Now is 2:20 A.M., I am not sleepy, this problem toss me a night, has not done right, finally found that the transfer equation ignores an important condition, tears rush ~.
Simply do not sleep, write a problem to alert themselves, but also for their own consideration of the mentally retarded punishment.
I'm a retarded 0 s 0 .....
The main idea is to give you n two-dimensional coordinates on the point (X, y), each point represents a stain, now has a width of W brush, parallel to the x-axis move K times, ask, up to how many stains can be erased.
Obviously the problem has nothing to do with the x-coordinate (because the brush moves along the parallel x-axis and can move infinitely far), analyzing the y-coordinate is OK. then record the y-coordinate after ordering, assuming that the array is POINT[1...N]. Direct DP Because the point range is too large for the sake of no way, so first discretization,Decode[1...len] storage of the point after discretization of the y-axis coordinates. OK, then it is easy to think of the transfer situation.
hahaha haha, simply ecstatic there is no, there is no! It's so hard to think, it's rare ....
DP[I][J] said that after I wiped the first I, after wiping the DECODE[1...J] (including j) after the maximum number of wiping off these stains, note is not necessarily wiped out all the stains (I was wrong here!!!) T ^ t)
The transfer equation is obvious, for each case J, consider the "upper bound" of the erase so the lower bound is the pre = Lower_bound (Decode,decode + len,decode[j]-W); The transfer situation is
DP[I][J] = max (Dp[i][j], dp[i-1][pre-1] + sum); (pre-1 >= 0)
DP[I][J] = max (dp[i][j], sum); (Pre-1 < 0)
Because the Lower_bound function is used, if the value of the search is less than the minimum value of decode, it will be 0,pre-1 meaningless, so the classification.
The sum here is obviously all the stains in the upper and lower bounds (including the upper and lower bounds).
Then the upper-bound position is
Upper_bound (Point, point + N, decode[j])-point;
Nether position in
Lower_bound (Point, point + N, Decode[j] – W)-point;
sum = upper bound-lower bound;
And then!!!
And then!!!!
Isn't it as if it's OK!!
But......
But my mother's AC!!!!
Not, the pain of WA began, keep the wrong wrong wrong wrong wrong, want to crash computer have no, there is no ...
I don't know what the situation is.
Don't sleep without doing it ....
Mom and you not finished!!!
Finally, at about 1:30 A.M. Enlightened.
Found the transfer equation of the fault, if the removal of the stain is divided into two segments, the middle of the stain "not selected" when the optimal solution, then the transfer will be wrong.
What do you mean, that is, our equation dp[i][j] is to clean up I once, the height of the stain below the J stain has been considered, the maximum after this, only the meaning of transfer.
meaning dp[i][j] = max{dp[i][1....j]}
So after each transfer dp[i][j], you need to look at this dp[i][j] is not the current I, J of the optimal solution, that is, add a sentence.
DP[I][J] = max (Dp[i][j], dp[i][j-1]);
What if dp[i][j-1] is better? In case the height of the stain of J I don't, clean can more?
Finally, right!
My mother can finally sleep!!!
Lying trough, three o ' ... Orz .....
Code June:
1 /*2 3 Light OJ 10174 5 */6 7 8#include <iostream>9#include <cstdio>Ten#include <cstring> One#include <algorithm> A using namespacestd; - intpoint[ the]; - intdecode[ the]; the intdp[ the][ the]; - int_decode (intN) { - intp =1; - intPre = point[0]; +decode[0] = point[0]; - for(inti =1; I < n; i++) { + if(pre = = Point[i])Continue; Adecode[p++] =Point[i]; atPre =Point[i]; - } - returnp; - } - intMain () { - intT; inscanf"%d",&T); - for(inttt =1; TT <= T; tt++) { to intN, W, K; +scanf" %d%d%d",&n,&w,&k); - for(inti =0; I < n; i++) { the inttemp; *scanf"%d%d", &temp, point +i); $ }Panax NotoginsengSort (point, point +n); - intLen =_decode (n); the intMIN = point[0]; +Memset (DP,0,sizeof(DP)); A for(inti =1; I <= K; i++) { the for(intj =0; J < Len; J + +) { + intPre = Lower_bound (Decode,decode + len,decode[j]-W)-decode; - intL = Lower_bound (point, point + N, Decode[j]-W)-Point ; $ intR = Upper_bound (point, point + N, decode[j])-Point ; $ if(Pre-1<0) Dp[i][j] = max (Dp[i][j], R-l); - ElseDP[I][J] = max (Dp[i][j], dp[i-1][pre-1] + (R-l)); - // theDP[I][J] = max (Dp[i][j], dp[i][j-1]); - //Key PartsWuyi } the } - intAns =0; WuAns = dp[k][len-1]; -cout <<" Case"<< TT <<": "<< ans <<Endl; About } $ return 0; -}
[DP] Light Oj 1017 Brush (III)