Monotone queue optimization DP || [NOI2005] magnificent waltz || BZOJ 1499 || Luogu P2254

Digression: Title Excellent, very poor experience do questions

Questions surface: [NOI2005] magnificent waltz

answer:

The maximum distance F [t] [i] [ j] denotes the time t piano is located (i, j) when
F [t] [i] [ j] = max (F [t-1] [i] [j] , F [t-1] [ a] [b] +1) (mp [i] [j] can be reached, (a, b) no furniture directly (i, j) between, i.e. legitimate path)
because the boat the inclination is continuous, it is possible to consider the time period that the DP
F [t] [i] [J] a rear end of the previous t time period, the piano is located (i, j) is the maximum distance
F [t] [i ] [j] = max (F [t] [i] [j], F [t-1] [a] [b] + Dis (a, b, i, j)) (mp [i] [j] up, Dis (a, b, i , j) <= T [t] -S [t] +1, (a, b) no furniture directly (i, j) between, i.e. path method)
consider monotonous queue optimization dp
the "OK" means mp [i] [j] no map, and no furniture on the (i, j), is a legitimate position can be reached, and the path of legitimate
path can be legitimate in the case of monotonous queue where the mp [i] [j] == ' x' to quickly direct the queue is empty, and definitely may also be implemented, and is determined by the write prefix
Note monotone write queue enqueue should be placed in maintenance F [t] [i ] [j] before, because it can stay in the (i, J)
case. 1: D [T]. 1 ==
case inclined ship north, then b = j (i large to small i)
F. [T] [i] [j] = max (F [ t] [i] [j], F [t-1] [a] [j] + ai) (OK, a i <= T [t] -S [t] +1)
i.e. Maintenance: max (F [t-1 ] [a] [j] + a) -i (a <= T [t] -S [t] i +. 1 +)
case 2: D [T] == 2
In this case the boat tilt southward, then b = j (i small to large i)
F [t] [i] [ j] = max (F [t] [i] [j], F [t-1] [a] [j] + ia) (OK, ia <= T [t] - S [t] +1)
i.e. maintenance: max (F. [. 1-T] [A] [J] -a) + I (A> = iT [T] + S [T] -1)
Case. 3: D [ t] == 3
In this case the boat tilted towards the west, then a = i (j descending)
F. [T] [I] [J] = max (F. [T] [I] [J], F. [T -1] [i] [b] + bj) (OK, bj <= T [t] -S [t] +1)
i.e. maintenance: max (F [t-1 ] [i] [b] + b) -j (B <T = [T] -S [T] +. 1 + J)
case. 4: D [T]. 4 ==
case boat east tilted, a = i (j small to large)
F. [T ] [i] [j] = max (F [t] [i] [j], F [t-1] [i] [b] + jb) (OK, jb <= T [t] -S [t ] +1)
i.e. maintenance: max (F [t-1 ] [i] [b] -b) + j (b> = jT [t] + S [t] -1)
of a queue to optimize the above monotonically

Code:

 1 #include<cstdio>
 2 #include<iostream>
 3 #define max(a,b) ((a)>(b)?(a):(b))
 4 #define min(a,b) ((a)<(b)?(a):(b))
 5 using namespace std;
 6 const int maxn=205,maxm=205,maxk=205,maxt=4e4+5;
 7 const int inf=1<<30;
 8 int N,M,K,X0,Y0,S[maxk],T[maxk],D[maxk];
 9 int F[maxk][maxn][maxm],f1,f2,ans=0;
10 char mp[maxn][maxm];
11 struct Node{ int data,x; }que[maxn];
12 int main(){
13     scanf("%d%d%d%d%d",&N,&M,&X0,&Y0,&K);
14     for(int i=1;i<=N;i++)
15         scanf("%s",mp[i]+1);
16     for(int i=1;i<=K;i++)
17         scanf("%d%d%d",&S[i],&T[i],&D[i]);
18     for(int t=0;t<=K;t++)
19         for(int i=0;i<=N;i++)
20             for(int j=0;j<=M;j++)
21                 F[t][i][j]=-inf;
22     for(int t=0;t<=K;t++)
23         F[t][X0][Y0]=0;
24     for(int t=1;t<=K;t++){
25         if(D[t]==1){
26             for(int j=1;j<=M;j++){
27                 f1=1,f2=0;            
28                 for(int i=N;i>=1;i--){
29                     if(mp[i][j]=='x') {
30                         f1=1,f2=0;
31                         continue;
32                     }
33                     while(f1<=f2 && que[f1].x>T[t]-S[t]+1+i) f1++;
34                     while(f1<=f2 && F[t-1][i][j]+i>=que[f2].data) f2--;
35                     que[++f2].data=F[t-1][i][j]+i; que[f2].x=i;
36                     if(f1<=f2) F[t][i][j]=max(F[t][i][j],que[f1].data-i);                    
37                 }
38             }
39         }
40         else if(D[t]==2){
41             for(int j=1;j<=M;j++){
42                 f1=1,f2=0;        
43                 for(int i=1;i<=N;i++){
44                     if(mp[i][j]=='x') {
45                         f1=1,f2=0;
46                         continue;
47                     }
48                     while(f1<=f2 && que[f1].x<i-T[t]+S[t]-1) f1++;
49                     while(f1<=f2 && F[t-1][i][j]-i>=que[f2].data) f2--;
50                     que[++f2].data=F[t-1][i][j]-i; que[f2].x=i;
51                     if(f1<=f2) F[t][i][j]=max(F[t][i][j],que[f1].data+i);                    
52                 }
53             }
54         }
55         else if(D[t]==3){
56             for(int i=1;i<=N;i++){
57                 f1=1,f2=0;
58                 for(int j=M;j>=1;j--){
59                     if(mp[i][j]=='x') {
60                         f1=1,f2=0;
61                         continue;
62                     }
63                     while(f1<=f2 && que[f1].x>T[t]-S[t]+1+j) f1++;
64                     while(f1<=f2 && F[t-1][i][j]+j>=que[f2].data) f2--;
65                     que[++f2].data=F[t-1][i][j]+j; que[f2].x=j;
66                     if(f1<=f2) F[t][i][j]=max(F[t][i][j],que[f1].data-j);                    
67                 }
68             }
69         }
70         else{// D[t]==4
71             for(int i=1;i<=N;i++){
72                 f1=1,f2=0;
73                 for(int j=1;j<=M;j++){
74                     if(mp[i][j]=='x') {
75                         f1=1,f2=0;
76                         continue;
77                     }
78                     while(f1<=f2 && que[f1].x<j-T[t]+S[t]-1) f1++;
79                     while(f1<=f2 && F[t-1][i][j]-j>=que[f2].data) f2--;
80                     que[++f2].data=F[t-1][i][j]-j; que[f2].x=j;
81                     if(f1<=f2) F[t][i][j]=max(F[t][i][j],que[f1].data+j);                    
82                 }
83             }
84         }
85     }
86     for(int i=1;i<=N;i++)
87         for(int j=1 ; j <= M; j ++ )
 88              years = max (years, F [K] [i] [j]);
89      printf ( " % d \ n " , year);
90      return  0 ;
91 }
View Code

By:AlenaNuna

Guess you like

Origin www.cnblogs.com/AlenaNuna/p/11569133.html