2021.08.12【NOIP提高B组】模拟 矩阵

在这里插入图片描述

思路:

先加入所有矩阵,然后每次扩散

c o d e code code

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<map>
#define num(i, j) (i-1)*m+j

using namespace std;

int n, m, ma, mb, k, a[1100][1100];
long long sum[1100][1100];
priority_queue<pair<long long, pair<int, pair<int, pair<int, int> > > > > q;
map<pair<int, int>, int> hash;

int main()
{
	scanf("%d%d%d%d%d", &n, &m, &ma, &mb, &k);
	for(int i=1; i<=n; i++)
		for(int j=1; j<=m; j++)
			scanf("%d", &a[i][j]), sum[i][j]=a[i][j]+sum[i-1][j]+sum[i][j-1]-sum[i-1][j-1];
	for(long long i=1; i<=n-ma+1; i++)
		for(long long j=1; j<=m-mb+1; j++)
			q.push(make_pair(-(sum[i+ma-1][j+mb-1]-sum[i+ma-1][j-1]-sum[i-1][j+mb-1]+sum[i-1][j-1]), make_pair(i, make_pair(j, make_pair(i+ma-1, j+mb-1)))));
	long long s;
	int x, y, x1, y1;
	k--;
	while(k--)
	{
		s=q.top().first, x=q.top().second.first, y=q.top().second.second.first, x1=q.top().second.second.second.first, y1=q.top().second.second.second.second;
		q.pop();
		if(x1<n&&!hash[make_pair(num(x, y), num(x1+1, y1))])
			q.push(make_pair(-(sum[x1+1][y1]-sum[x-1][y1]-sum[x1+1][y-1]+sum[x-1][y-1]), make_pair(x, make_pair(y, make_pair(x1+1, y1))))), hash[make_pair(num(x, y), num(x1+1, y1))]=1;
		if(y1<m&&!hash[make_pair(num(x, y), num(x1, y1+1))])
			q.push(make_pair(-(sum[x1][y1+1]-sum[x-1][y1+1]-sum[x1][y-1]+sum[x-1][y-1]), make_pair(x, make_pair(y, make_pair(x1, y1+1))))), hash[make_pair(num(x, y), num(x1, y1+1))]=1;
	}
	printf("%lld", -q.top().first);
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值