Luo Valley P2254 [NOI2005] magnificent Waltz (monotone stack optimization DP)

Title Description

Might think ballroom is a matrix of N rows and M columns, piling up some furniture in certain cells of the matrix, others are open space. Piano can slide on the ground, but can not hit the furniture or out of the ballroom, otherwise it will damage the piano and furniture, the captain drew tough. Each time, the piano will be with a sliding direction of the hull is inclined to the adjacent grid squares, the grid may be adjacent east, west, south or north. And Emily can choose to enchant or no enchant: If you do not do magic, the piano slides; if enchanted, the piano will stay put.

Emily is an angel, she knew inclination of the hull of each period. She wants to make the piano in the ballroom glide away as long as possible, so that 1900 will be very happy, but also conducive to the treatment of Tony's seasick. But Emily is too young, do not count, so I hope you can help her.

Input Format

The first line of the input file contains the 5 number N, M, x, y and K. Size N and M described dance halls, x and y for the initial position of the piano; we inclination of the hull is described by a time interval, and calculates a time from the start, such as "east [1, 3] time tilt, [4, 5] time north tilt ", therefore where K represents the number of sections.

The following N lines of M characters, describing the ballroom furniture. '.' Character i-th row j-th column if it is, it means that the position of the open space; if it is 'X', then furniture.

The following lines K, K time intervals described in order, in the format: si ti di (1 ≤ i ≤ K). Represents the time interval [si, ti] the hull is inclined in the direction di. di is 1, 2, 3, 4 a, respectively for the North, South, West, East (corresponding to the matrix, down, left, right). To ensure that the input section is continuous, i.e.,

s1 = 1 ti = si-1 + 1 (1 <i ≤ K)

tK = T

Output Format

The output file is only one line, contains an integer representing the piano glide longest distance (ie, the number of grid).

Sample input and output

Input # 1
4 5 4 1 3
..xx.
.....
...x.
.....
1 3 4
4 5 1
6 7 3
Output # 1
6

Description / Tips

Piano taxi routes:

Angel piano at once magic "×" position, the total length of the slide 6.

【data range】

50% of the data, 1≤N, M≤200, T≤200;

100% of the data, 1≤N, M≤200, K≤200, T≤40000.


 

Meaning of the title: the title says very clearly, is to give you a free map, there are obstacles 'x', piano starting position sx, sy, you give K inputs indicating piano at time t s to be. '' slide the piano d direction, but you can apply the magic, is not sliding at a certain time, ask you how many pianos can slide up to the grid;

Solution: First, consider the time t to DP: F [ t] [ i] [ j] indicates the j-th column of time t available to the longest distance i-th row. Transfer equation: F [ T] [ I] [ J] = max (F [ T-. 1] [ I] [ J], F [ T] [ I *] [ J *] +. 1) (I *, J * ) but 100% TLE and MLE is a reasonable position. It must be optimized, the first time interval t into k, so that F [ k] [ I] [ J] represents the k-th segment coasting interval at positions i, j noted that the longest distance that can be obtained at time k-th segment You can only go in one direction within a maximum of x step (x is the length of the interval), to give the transfer equation F [ K] [ I] [ J] = max (F [ K-. 1] [ I] [ J], F [ K ] [ I *] [ j *] + DIS (I, J, I *, j *)) (I *, the time complexity of a reasonable position j * of) this approach is O (kn ^ 3), when the time out, need further optimization to optimize away a n inner monotonically queue can be done O (kn ^ 2), can be AC, this code is also used to scroll the array optimized by selecting the maximum value monotonically decreasing queue, met obstacles to the cleared the queue, comparing the queue when the need to add additional offset dis.

 

Reference Code:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pii pair<int,int>
#define pil pair<int,ll>
#define mkp make_pair
#define fi first
#define se second
const int INF=0x3f3f3f3f;
inline int read()
{
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-') f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}
    return x*f;
}
const int maxn=210;
pii q[maxn];
int n,m,sx,tx,K,ans,dp[maxn][maxn];
char G[maxn][maxn];
int dx[5]={0,-1,1,0,0},dy[5]={0,0,0,-1,1};
inline void work(int x,int y,int len,int d)
{
    int l=0,r=0;
    for(int i=1;x>=1&&x<=n&&y>=1&&y<=m;++i,x+=dx[d],y+=dy[d])
    {
        if(G[x][y]=='x') l=r=0;
        else
        {
            while(l<r&&q[r-1].fi+i-q[r-1].se<dp[x][y]) --r;
            q[r++]=mkp(dp[x][y],i);
            if(q[r-1].se-q[l].se>len) ++l;
            dp[x][y]=q[l].fi+i-q[l].se;
            ans=max(ans,dp[x][y]);
        }
    }
}
int main()
{
    n=read();m=read();sx=read();tx=read();K=read();
    for(int i=1;i<=n;++i) scanf("%s",G[i]+1);
    memset(dp,-INF,sizeof dp);
    dp[sx][tx]=0;
    for(int j=1;j<=K;++j)
    {
        int s,t,d,len;
        s=read();t=read();d=read();
        len=t-s+1;
        if(d==1) for(int i=1;i<=m;++i) work(n,i,len,d);
        if(d==2) for(int i=1;i<=m;++i) work(1,i,len,d);
        if(d==3) for(int i=1;i<=n;++i) work(i,m,len,d);
        if(d==4) for(int i=1;i<=n;++i) work(i,1,len,d);
    }
    printf("%d\n",ans);

    return 0;
}
View Code

 

Guess you like

Origin www.cnblogs.com/songorz/p/11343802.html