BZOJ 4716 假摔

我们发现我们可以对每一个点维护其还没有用过的矩形中最小的几个,记得判重。

#pragma GCC optimize("-O2")
#include<bits/stdc++.h>
using namespace std;
#define sight(c) ('0'<=c&&c<='9')
#define LL long long
#define M 1007
inline void read(int &x){
    static char c; static int b;
    for (b=1,c=getchar();!sight(c);c=getchar()) if (c=='-') b=-1;
    for (x=0;sight(c);c=getchar())x=x*10+c-48; x=x*b;
}
LL sum[M][M];
struct state{
    int x,y,lx,ly;
    LL ans;
    state() {}
    state(int _x,int _y,int _lx,int _ly):x(_x),y(_y),lx(_lx),ly(_ly){
        ans=sum[_x-1+lx][_y-1+ly]+sum[_x-1][_y-1]-sum[_x-1+lx][_y-1]-sum[_x-1][_y-1+ly];
    }
    inline bool operator <(const state& P)const&{
        return
         P.ans==ans?
          (P.x==x?
           (P.y==y?
            (P.lx==lx?
              P.ly<ly:
               P.lx<lx):
                P.y<y):
                 P.x<x):  
                  P.ans<ans;
    }
}Now;
#define N 2346007
state hep[N];
int len,n,m,xm,ym,K,ax;
map<state,bool> mp;
void write(LL x){if (x<10) {putchar('0'+x); return;} write(x/10); putchar('0'+x%10);}
inline void writeln(LL x){ if (x<0) putchar('-'),x*=-1; write(x); putchar('\n'); }
inline void writel(LL x){ if (x<0) putchar('-'),x*=-1; write(x); putchar(' '); }
void put(state P){hep[++len]=P,push_heap(hep+1,hep+1+len);}
inline state get() {pop_heap(hep+1,hep+1+len);return hep[len--];}
signed main () {
    read(n); read(m); read(xm); read(ym); 
    read(K);
    for (int i=1;i<=n;i++)
     for (int j=1;j<=m;j++) read(ax),sum[i][j]=sum[i-1][j]+sum[i][j-1]-sum[i-1][j-1]+ax;
    for (int i=1;i<=n-xm+1;i++)
     for (int j=1;j<=m-ym+1;j++)  put(state(i,j,xm,ym));
//  for (int i=1;i<=len;i++) hep[i].we();
    while (K--) {
        while (Now=get(),1) {
            if (!mp[Now]) break;
        }
        mp[Now]=1;
//      writel(Now.x); writel(Now.y); writel(Now.lx); writel(Now.ly); writeln(Now.ans);
        if (Now.x+Now.lx<=n) put(state(Now.x,Now.y,Now.lx+1,Now.ly));
        if (Now.y+Now.ly<=m) put(state(Now.x,Now.y,Now.lx,Now.ly+1));
    }
    writeln(Now.ans+1); return 0;
}

 

posted @ 2018-07-02 21:30  泪寒之雪  阅读(137)  评论(0编辑  收藏  举报