BZOJ 4165 矩阵 堆

先把每个长为$mina$,宽为$minb$的矩阵扔到堆里,然后由于矩阵中的数都是正的,所以我们每取出来一个矩形,,就把他向四个方向扩张一行,再把这新的且更大的四个矩形扔到堆里。注意判重,于是我比较懒用的$map$ $qwq$

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<cctype>
#include<cstdlib>
#include<vector>
#include<queue>
#include<map>
#include<set>
#define ull unsigned long long
#define ll long long
#define R register int
using namespace std;
namespace Fread {
    static char B[1<<15],*S=B,*D=B;
    #define getchar() (S==D&&(D=(S=B)+fread(B,1,1<<15,stdin),S==D)?EOF:*S++)
    inline int g() {
        R ret=0,fix=1; register char ch; while(!isdigit(ch=getchar())) fix=ch=='-'?-1:fix;
        do ret=ret*10+(ch^48); while(isdigit(ch=getchar())); return ret*fix;
    } inline bool isempty(const char& ch) {return ch<=36||ch>=127;}
    inline void gs(char* s) {register char ch; while(isempty(ch=getchar())); do *s++=ch; while(!isempty(ch=getchar()));}
}using Fread::g; using Fread::gs;
struct node {
    int x1,y1,x2,y2; ll W; node() {} node(int xx,int yy,int xxx,int yyy,ll w) {x1=xx,y1=yy,x2=xxx,y2=yyy,W=w;}
    inline bool operator < (const node& that) const {return W>that.W;}
}; priority_queue<node> q;
map<unsigned long long,bool> v;
inline ull hsh(int x1,int y1,int x2,int y2) {return x1+y1*1001+x2*1000001ull+y2*1000000001ull;}
const int dx1[]={0,0,0,-1},dx2[]={0,1,0,0},dy1[]={-1,0,0,0},dy2[]={0,0,1,0};
int n,m,a,b,k; int w[1010][1010]; ll sum[1010][1010];
inline bool ck(int x1,int y1,int x2,int y2) { return x1<1||x1>n||x2<1||x2>n||y1<1||y1>m||y2<1||y2>m;}
signed main() {
#ifdef JACK
    freopen("NOIPAK++.in","r",stdin);
#endif
    n=g(),m=g(),a=g(),b=g(),k=g();
    for(R i=1;i<=n;++i) for(R j=1;j<=m;++j) w[i][j]=g();
    for(R i=1;i<=n;++i) for(R j=1;j<=m;++j) sum[i][j]=sum[i][j-1]+sum[i-1][j]-sum[i-1][j-1]+w[i][j];
    for(R i=a;i<=n;++i) for(R j=b;j<=m;++j) 
        q.push(node(i-a+1,j-b+1,i,j,(ll)sum[i][j]-sum[i-a][j]-sum[i][j-b]+sum[i-a][j-b])),v[hsh(i-a+1,j-b+1,i,j)]=true;
    for(R i=1;i<k;++i) { register node tmp=q.top(); q.pop(); R x1=tmp.x1,x2=tmp.x2,y1=tmp.y1,y2=tmp.y2;
        for(R i=0;i<=3;++i) { R X1=x1+dx1[i],Y1=y1+dy1[i],X2=x2+dx2[i],Y2=y2+dy2[i]; if(ck(X1,Y1,X2,Y2)) continue;
        register ull H=hsh(X1,Y1,X2,Y2); 
            if(v.find(H)==v.end()) q.push(node(X1,Y1,X2,Y2,sum[X2][Y2]-sum[X2][Y1-1]-sum[X1-1][Y2]+sum[X1-1][Y1-1])),v[H]=true;
        }
    } printf("%lld\n",q.top().W);
}

2019.06.13

posted @ 2019-06-13 23:32  LuitaryiJack  阅读(166)  评论(0编辑  收藏  举报