POJ 2430 Lazy Cows pressure DP

Source: Internet
Author: User

This topic is still relatively easy to think of using the state DP to solve, but the state of the transfer is more troublesome, and with the discretization, more prone to error.

Dp[i][j][k] means that the first column is covered with a J rectangle, and the current coverage state is the optimal solution of K.

K==1: Cover number 1th lattice

K==2: Cover number 2nd lattice

K==3: Covers 1, 2nd, and is the same rectangle

K==4: Covers 1, 2nd, and is a different rectangle

Then you need to consider the transition, create a new rectangle, or simply extend the current rectangle's edge length. There are more kinds of transfers.

#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace
Std
const int maxn=1e3+9;
BOOL A[3][MAXN];
int X[MAXN],Y[MAXN],F[MAXN],G[MAXN];
int dp[maxn][maxn][5];
    struct D {int id,key;
    BOOL operator < (const D & XX) const {return key<xx.key;
}}H[MAXN];
    int main () {//Freopen ("In.txt", "R", stdin);
    int n,k,b;
        while (scanf ("%d%d%d", &n,&k,&b)!=eof) {memset (a,0,sizeof (a));
            for (int i=1;i<=n;i++) {scanf ("%d%d", &x[i],&y[i]);
        H[i].key=y[i],h[i].id=i;
        } sort (h+1,h+1+n);
        int top=0;
        F[h[1].id]=++top;
        G[top]=h[1].key;
            for (int i=2;i<=n;i++) {if (H[i].key!=h[i-1].key) ++top;
            F[h[i].id]=top;
        G[top]=h[i].key;

        } for (int i=1;i<=n;i++) a[x[i]][f[i]]=1;
        Memset (Dp,50,sizeof (DP)); dp[0][0][0]=0; 
                for (int k=0;k<top;k++) {for (Int. i=0;i<=k;i++) for (int j=0;j<5;j++) {

                if (a[1][k+1]==0) dp[k+1][i+1][2]=min (dp[k+1][i+1][2],dp[k][i][j]+1);

                if (a[2][k+1]==0) dp[k+1][i+1][1]=min (dp[k+1][i+1][1],dp[k][i][j]+1);
                Dp[k+1][i+1][3]=min (dp[k+1][i+1][3],dp[k][i][j]+2);

                Dp[k+1][i+2][4]=min (dp[k+1][i+2][4],dp[k][i][j]+2); if (j==1) {if (a[2][k+1]==0) dp[k+1][i][j]=min (dp[k+1][i][j],dp[k][
                    I][j]+g[k+1]-g[k]);
                else Dp[k+1][i+1][4]=min (dp[k+1][i+1][4],dp[k][i][j]+g[k+1]-g[k]+1); } else if (j==2) {if (a[1][k+1]==0) dp[k+1][i][j]=m
                    In (Dp[k+1][i][j],dp[k][i][j]+g[k+1]-g[k]); else Dp[k+1][i+1][4]=min (dp[k+1][i+1][4],DP[K][I][J]+G[K+1]-G[K]+1); } else if (j==3) {dp[k+1][i][j]=min (dp[k+1][i][j],dp[k][i][j]+ (g[k+1]-
                G[k]); } else if (j==4) {dp[k+1][i][j]=min (dp[k+1][i][j],dp[k][i][j]+ (g[k+1]-

                    G[k]);
                    if (a[2][k+1]==0) dp[k+1][i][1]=min (Dp[k+1][i][1],dp[k][i][j]+g[k+1]-g[k]);

                    else Dp[k+1][i+1][4]=min (dp[k+1][i+1][4],dp[k][i][j]+g[k+1]-g[k]+1);
                    if (a[1][k+1]==0) dp[k+1][i][2]=min (Dp[k+1][i][2],dp[k][i][j]+g[k+1]-g[k]);
                else Dp[k+1][i+1][4]=min (dp[k+1][i+1][4],dp[k][i][j]+g[k+1]-g[k]+1);

        }}} int ans=1e9;
        for (int. k=0;k<=k;k++) for (int i=0;i<5;i++) ans=min (ans,dp[top][k][i]+k-k);
    cout<<ans<<endl; } RETurn 0;
 }


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.