Attach the link to this question: http://poj.org/problem?id=1769
The title means that there is a device can output the maximum number of n, this device is composed of M sequencer, each sequencer can be the number of n from S to T in order from small to large, there is a person found in M sequencer some of the sequencer removed still does not affect the function, Now ask how many sequencer you need at least to complete the function. We can define the number of dp[i][j] for the first I-sequencer to refer to the 1th number of the minimum sequencer required by J, then when t[i] = = J Dp[i][j] = min (dp[i-1][j], min (Dp[i-1][si-ti]) + 1) when Ti! = J when dp[i][j] = Dp[i-1][j], we observe that the number of States has m*n, obviously the direct solution will time out, so we consider using a line segment tree to optimize the design of the Challenge program p207, the code is as follows:
#include <cstdio>#include<cstring>#include<algorithm>using namespacestd;Const intMAXN =500000+ -;Const intINF =0x3f3f3f3f;intN, M;structsegment{intL, R; intx;} seg[3*MAXN];voidBuildintRtintLintr) {SEG[RT].L= L; SEG[RT].R =R; if(L = =r) {intValue =inf; if(L = =1) value =0; seg[rt].x=value; return ; } intCHL =2*RT, CHR =2*rt +1; intMid = (L + r)/2; Build (CHL, L, mid); Build (CHR, Mid+1, R); seg[rt].x=min (seg[chl].x, seg[chr].x);}intQueryintRtintLintr) {if(SEG[RT].L = = L && SEG[RT].R = =r) {returnseg[rt].x; } intMid = (SEG[RT].L + SEG[RT].R)/2; if(R <=mid)returnQuery2*RT, L, R); Else if(L >mid)returnQuery2*rt+1, L, R); Else{ intV1 = Query (2*RT, L, mid); intV2 = Query (2*rt+1, mid+1, R); returnmin (v1, v2); }}voidUpdateintRtintIintc) {if(SEG[RT].L==SEG[RT].R && SEG[RT].L = =i) {seg[rt].x=C; return ; } intMid = (SEG[RT].L + SEG[RT].R)/2; if(I <=mid) Update (2*RT, I, c); ElseUpdate (2*rt+1, I, c); seg[rt].x= Min (seg[2*rt].x, seg[2*rt+1].x);}intMain () { while(SCANF ("%d%d", &n, &m)! =EOF) {Build (1,1, N); for(intI=0; i<m; i++) { ints, t; scanf ("%d%d", &s, &t); intV1 = Query (1, S, t) +1; intV2 = Query (1, T, T); intV3 =min (v1, v2); Update (1, T, V3); } printf ("%d\n", Query (1, N, N)); } return 0;}
poj1769 segment Tree Optimized DP