• 最大加权矩形 luogu1719


    题目链接:https://www.luogu.org/problemnew/show/P1719

    这道题挺好做的 又是一道练前缀和的题

    #include <bits/stdc++.h>
    #define Max(a,b) a>b?a:b
    #define rep(i,j,n) for(register int i=j;i<=n;i++)
    using namespace std;
    typedef long long LL;
    inline LL read() { LL x=0; int f=1; char ch=getchar();
        while(!isdigit(ch)) { if (ch=='-') f=-1; ch=getchar();}
        while(isdigit(ch)) x=(x<<1)+(x<<3)+(ch^48),ch=getchar(); return x*f;
    }
    int n,m;
    const int N=1<<7;
    int a[N][N];
    signed main(){
        n=read();
        rep(i,1,n) rep(j,1,n) a[i][j]=read()+a[i-1][j]+a[i][j-1]-a[i-1][j-1];
        LL ans=0;
        rep(x1,1,n) rep(y1,1,n) rep(x2,x1+1,n) rep(y2,y1+1,n) ans=Max(ans,a[x2][y2]-a[x1-1][y2]-a[x2][y1-1]+a[x1-1][y1-1]);
        cout << ans << endl ;
        return 0;
    }

    前缀和的代码


    同样 这需要DP来降低时间复杂度 提高效率orz

    #include <bits/stdc++.h>
    using namespace std;
    typedef long long LL;
    inline LL read () { LL res = 0 ;int f (1) ;char ch = getchar ();
        while (!isdigit(ch)) { if (ch == '-') f = -1 ;ch = getchar();}
        while (isdigit(ch)) res = (res << 1) + (res << 3) + (ch ^ 48) ,ch = getchar(); return res * f ;
    }
    int n,s[121][121],f[121],ans,p,x,ma;
    signed main() {
        n=read();
        for(register int i=1; i<=n; i++) f[i]=-1e9;
        for(register int i=1; i<=n; i++)
            for(register int j=1; j<=n; j++) {
                x=read();
                maxn=Max(maxn,x);
                s[i][j]=s[i-1][j]+x;
            }
        ans=-1e9;
        for(register int i=1; i<=n; i++)
            for(register int j=i; j<=n; j++)
                for(register int k=1; k<=n; k++) {
                    p=s[j][k]-s[i-1][k]; f[k]=max(p,f[k-1]+p); ans=max(ans,f[k]);
                }
        cout << ans << endl ;
        return 0;
    }
    不存在十全十美的文章 如同不存在彻头彻尾的绝望
  • 相关阅读:
    滚动图片
    Iframe自动适应高度
    我的生活,我的精彩!
    静下心来
    写给关心我的人
    关于考研
    按时间自动刷新页面
    破除网页鼠标右键禁用的十大绝招
    DotNetNuke 皮肤制作白皮书
    WollOp
  • 原文地址:https://www.cnblogs.com/qf-breeze/p/10426486.html
Copyright © 2020-2023  润新知