zoukankan      html  css  js  c++  java
  • UVA 1366 Martian Mining DP

    为了方便,记从右到左运输的为A矿,从下到上运输的为B矿。

    首先,假如我们在第i行的前k格架了运输管道运输这K个格子的A矿,那么对于i下面的其他行最少都能架上k格管子,因为不架也是浪费,这一片区域的B矿已经不可能运输了,都被i行的管道挡住了。

    基于这点,当第i行前K格用来运输A矿时,剩下的第K+1,K+2...M格就能用来运输B矿,因为上面不会被挡着。

    dp[i][j]表示第i行,且前面最长的A型管(左右型)为j的状态。

    则dp[i][j]=max(dp[i+1][k]+f[i][k+1],j<=k<=m) ,f[i][j]为预处理出的B矿每行的后缀和。

    //#pragma comment(linker, "/STACK:1024000000,1024000000")
    #include<cstdio>
    #include<cstring>
    #include<cstdlib>
    #include<algorithm>
    #include<iostream>
    #include<sstream>
    #include<cmath>
    #include<climits>
    #include<string>
    #include<map>
    #include<queue>
    #include<vector>
    #include<stack>
    #include<set>
    using namespace std;
    typedef long long ll;
    typedef pair<int,int> pii;
    #define pb(a) push_back(a)
    #define INF 0x1f1f1f1f
    #define lson idx<<1,l,mid
    #define rson idx<<1|1,mid+1,r
    #define PI  3.1415926535898
    template<class T> T min(const T& a,const T& b,const T& c) {
        return min(min(a,b),min(a,c));
    }
    template<class T> T max(const T& a,const T& b,const T& c) {
        return max(max(a,b),max(a,c));
    }
    void debug() {
    #ifdef ONLINE_JUDGE
    #else
    
        freopen("d:\in.txt","r",stdin);
        freopen("d:\out1.txt","w",stdout);
    #endif
    }
    int getch() {
        int ch;
        while((ch=getchar())!=EOF) {
            if(ch!=' '&&ch!=' ')return ch;
        }
        return EOF;
    }
    int n,m;
    int a[550][550],b[550][550];
    int dp[550][550];
    int f(int k,int maxl)
    {
        if(dp[k][maxl]>=0)return dp[k][maxl];
        if(k>n)return 0;
        int num=0;
        int maxx=-1;
        for(int i=0;i<=m;i++)
        {
            num+=a[k][i];
            if(i>=maxl)
                maxx=max(maxx,f(k+1,i)+num+b[k][i+1]);
        }
        return dp[k][maxl]=maxx;
    }
    int main()
    {
        while(scanf("%d%d",&n,&m)!=EOF&&(n||m))
        {
            for(int i=1;i<=n;i++)
                for(int j=1;j<=m;j++)
                    scanf("%d",&a[i][j]);
            for(int i=1;i<=n;i++)
                for(int j=1;j<=m;j++)
                    scanf("%d",&b[i][j]);
            for(int i=1;i<=n;i++)b[i][m+1]=0;
            for(int i=1;i<=n;i++)
                for(int j=m-1;j>=1;j--)
                    b[i][j]+=b[i][j+1];
            memset(dp,-1,sizeof(dp));
            printf("%d
    ",f(1,0));
        }
        return 0;
    }
    View Code
  • 相关阅读:
    hdu2328 Corporate Identity
    hdu1238 Substrings
    hdu4300 Clairewd’s message
    hdu3336 Count the string
    hdu2597 Simpsons’ Hidden Talents
    poj3080 Blue Jeans
    poj2752 Seek the Name, Seek the Fame
    poj2406 Power Strings
    hust1010 The Minimum Length
    hdu1358 Period
  • 原文地址:https://www.cnblogs.com/BMan/p/3272811.html
Copyright © 2011-2022 走看看