CodeForces - 15D Map【前缀和】【单调队列】

题目链接:https://codeforces.com/contest/15/problem/D

很显然花费为 S u m 子 矩 阵 − a ∗ b ∗ m i n 子 矩 阵 Sum_{子矩阵}-a*b*min_{子矩阵} Sumabmin
S用前缀和处理,min用单调队列处理
排个序,判个重就好了
常数写的略大,加个快读能卡过去
早期的cf的题标的属实离谱,这个题绝对到不了2500的难度。。

#include <iostream>
#include <cstring>
#include <algorithm>
#include <deque>
using namespace std;
typedef pair<int,int> PII;
static const int MAXN=1000+10;
struct Node{
    long long cost;
    int x,y;
    bool select;
    bool operator<(const Node &b)const{
        if(cost==b.cost)
        {
            if(x==b.x) return y<b.y;
            return x<b.x;
        }
        return cost<b.cost;
    }
}ansseq[MAXN*MAXN];
int cnt;
int h[MAXN][MAXN];
long long s[MAXN][MAXN];
bool vis[MAXN][MAXN];
int n,m,a,b;
inline int read(){ int s=0,w=1; char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();} while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar(); return s*w; }
int main()
{
    n=read(),m=read(),a=read(),b=read();
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
        {
            h[i][j]=read();
            s[i][j]=s[i-1][j]+s[i][j-1]-s[i-1][j-1]+h[i][j];
        }
    for(int i=1;i<=n;i++)
    {
        deque<PII> dq;
        for(int j=1;j<=m;j++)
        {
            while(!dq.empty() && dq.front().second+b-1<j) dq.pop_front();
            while(!dq.empty() && dq.back().first>=h[i][j]) dq.pop_back();
            dq.push_back({h[i][j],j});
            h[i][j]=dq.front().first;
        }
    }
    for(int i=1;i<=m;i++)
    {
        deque<PII> dq;
        for(int j=1;j<=n;j++)
        {
            while(!dq.empty() && dq.front().second+a-1<j) dq.pop_front();
            while(!dq.empty() && dq.back().first>=h[j][i]) dq.pop_back();
            dq.push_back({h[j][i],j});
            h[j][i]=dq.front().first;
        }
    }
    for(int i=1;i+a-1<=n;i++)
        for(int j=1;j+b-1<=m;j++)
            ansseq[cnt++]={s[i+a-1][j+b-1]-s[i+a-1][j-1]-s[i-1][j+b-1]+s[i-1][j-1]-1ll*a*b*h[i+a-1][j+b-1],i,j,0};
    sort(ansseq,ansseq+cnt);
    int res=0;
    for(int i=0;i<cnt;i++)
    {
        int x1=ansseq[i].x,y1=ansseq[i].y,x2=ansseq[i].x+a-1,y2=ansseq[i].y+b-1;
        if(!vis[x1][y1] && !vis[x1][y2] && !vis[x2][y1] && !vis[x2][y2])
        {
            res++;
            ansseq[i].select=true;
            for(int i=x1;i<=x2;i++)
                for(int j=y1;j<=y2;j++)
                    vis[i][j]=true;
        }
    }
    printf("%d\n",res);
    for(int i=0;i<cnt;i++)
        if(ansseq[i].select)
            printf("%d %d %lld\n",ansseq[i].x,ansseq[i].y,ansseq[i].cost);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值