1. 程式人生 > 實用技巧 >最大菱形和

最大菱形和

20200915

如果是正方形,字首和即可

現在是菱形,就將座標變換 (x,y)->(x+y,x-y)

可以發現菱形變成了正方形,字首和

#include<bits/stdc++.h>
#define ll long long
const ll INF=6e18;
using namespace std;

    long long Seed,a,b,c,MOD;
    long long Random() {
        c++;
        Seed=(Seed*a+b*c)%MOD;
        return Seed;
    }
    int Random_int() {
        
int tmp=Random(); if(Random()&1)tmp*=-1; return tmp; } const int N=6005,M=3005; int A[M][M],B[N][N],n,m,h; ll s[N][N]; void MAKE(){ scanf("%lld%lld%lld%lld%lld",&Seed,&a,&b,&c,&MOD); for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) A[i][j]
=Random_int(); } int main() { freopen("rhombus.in","r",stdin); freopen("rhombus.out","w",stdout); int ty; scanf("%d%d%d%d",&n,&m,&h,&ty); if(!ty) { for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) scanf("%d",&A[i][j]); } else MAKE();
for(int i=1;i<=n;i++) { for(int j=1;j<=m;j++) { B[i+j][i-j+m]=A[i][j]; } } for(int i=1;i<=n+m;i++) { for(int j=1;j<=n+m;j++) { s[i][j]=s[i-1][j]+s[i][j-1]-s[i-1][j-1]+B[i][j]; } } ll ans=-INF; for(int i=1;i<=n;i++) { if(i-h+1<=0||i+h-1>n) continue; for(int j=1;j<=m;j++) { if(j-h+1<=0||j+h-1>m) continue; int X2=i+j+h-1,X1=i+j-h+1,Y2=i-j+h-1+m,Y1=i-j-h+1+m; ans=max(ans,s[X2][Y2]-s[X2][Y1-1]-s[X1-1][Y2]+s[X1-1][Y1-1]); } } printf("%lld\n",ans); return 0; }