跳到正文

题解

题解:P12561 [UTS 2024] Matrix

__liujy约 13 分钟
原文
文章信息与版本

文章信息

发布
2026年9月19日
收录
2026年9月19日
更新
2026年11月13日
篇幅
约 13 分钟
题目
P12561

版本

  1. v1当前

P12561 [UTS 2024] Matrix

这期有教育意义。

首先对于边长为 kk 的三角形,当 kk 为奇数时,可以分为一个边长为 ⌈k2⌉\lceil \frac{k}{2} \rceil 的正方形和两个边长为 ⌊k2⌋\lfloor \frac{k}{2} \rfloor 的三角形;当 kk 为偶数时,可以分为一个边长为 k2\frac{k}{2} 的正方形和两个边长为 k2\frac{k}{2} 的三角形。整理一下,对于一个边长为 kk 的三角形,可以被分为一个边长为 ⌈k2⌉=⌊k+12⌋\lceil \frac{k}{2} \rceil = \lfloor \frac{k + 1}{2} \rfloor 的正方形,和两个边长为 ⌊k2⌋\lfloor \frac{k}{2} \rfloor 的三角形。

对于正方形的部分,可以先对行做单调队列,再对列做单调队列,时间复杂度为 O(nm)O(nm)。

对于三角形的部分,我们将三角形的边长写下来,形如:

k,⌊k2⌋,⌊⌊k2⌋2⌋,⋯ ,1k, \lfloor \frac{k}{2} \rfloor, \lfloor \frac{\lfloor \frac{k}{2} \rfloor}{2} \rfloor, \cdots, 1

注意到这么递归下去三角形的边长最后一定为 11,而对于边长为 11 的三角形最大值就是它本身。

将这个三角形的边长记下来,然后按照上文对边长为 kk 的三角形的分讨算即可,时间复杂度为 O(nmlog⁡k)O(nm \log k)。

把卡完常的代码放出来:

C++
#include<bits/stdc++.h>

const int MAXN = 2000 + 5, MAXV = 1e6 + 5;
#define pb push_back
typedef long long LL;

int n, m, k, a[MAXN][MAXN];

int tot, route[MAXN];

int suf[MAXN][MAXN], count[MAXN][MAXN], box[MAXV], f[MAXN][MAXN][2], g[MAXN][MAXN][2];

int head, tail, dq[MAXN];

static char ibuf[1 << 20], *ip = ibuf, *iend = ibuf;
inline int gc() { if (ip == iend) iend = (ip = ibuf) + fread(ibuf, 1, 1 << 20, stdin); return ip == iend ? EOF : *ip++; }
const int BUFSIZE = 1 << 20;
char obuf[BUFSIZE], *op = obuf;
inline void flush() { fwrite(obuf, 1, op - obuf, stdout); op = obuf; }
inline void pc(char c) { if (op == obuf + BUFSIZE) flush(); *op++ = c; }
inline bool blank(const char x) { return x == ' ' || x == '\n' || x == '\r' || x == '\t'; }
template<typename Tp> inline void read(Tp &x) { x = 0; bool z = true; int a = gc(); for(; !('0' <= a && a <= '9'); a = gc()) if(a == '-') z = false; for(; ('0' <= a && a <= '9'); a = gc()) x = (x << 1) + (x << 3) + (a ^ 48); x = (z ? x : ~x + 1); }
inline void read(double &x) { x = 0.0; bool z = true; double y = 0.1; int a = gc(); for(; !('0' <= a && a <= '9'); a = gc()) if(a == '-') z = false; for(; ('0' <= a && a <= '9'); a = gc()) x = x * 10 + (a ^ 48); if(a != '.') return x = z ? x : -x, void(); for(a = gc(); ('0' <= a && a <= '9'); a = gc(), y /= 10) x += y * (a ^ 48); x = (z ? x : -x); }
inline void read(char &x) { for(x = gc(); blank(x) && (x ^ -1); x = gc()); }
inline void read(char *x) { char a = gc(); for(; blank(a) && (a ^ -1); a = gc()); for(; !blank(a) && (a ^ -1); a = gc()) *x++ = a; *x = 0; }
inline void read(std::string &x) { x = ""; char a = gc(); for(; blank(a) && (a ^ -1); a = gc()); for(; !blank(a) && (a ^ -1); a = gc()) x += a; }
template<typename T, typename ...Tp> inline void read(T &x, Tp &...y) { read(x), read(y...); }
template<typename Tp> inline void write(Tp x) { if(!x) return pc(48), void(); if(x < 0) pc('-'), x = ~x + 1; int len = 0; char tmp[64]; for(; x; x /= 10) tmp[++len] = x % 10 + 48; while(len) pc(tmp[len--]); }
inline void write(const double x) { int a = 6; double b = x, c = b; if(b < 0) pc('-'), b = -b, c = -c; double y = 5 * powl(10.0, -a - 1); b += y, c += y; int len = 0; char tmp[64]; if(b < 1) pc(48); else for(; b >= 1; b /= 10) tmp[++len] = std::floor(b) - std::floor(b / 10) * 10 + 48; while(len) pc(tmp[len--]); pc('.'); for(c *= 10; a; a--, c *= 10) pc(std::floor(c) - std::floor(c / 10) * 10 + 48); }
inline void write(const std::pair<int, double> x) { int a = x.first; if(a < 7) { double b = x.second, c = b; if(b < 0) pc('-'), b = -b, c = -c; double y = 5 * powl(10.0, -a - 1); b += y, c += y; int len = 0; char tmp[64]; if(b < 1) pc(48); else for(; b >= 1; b /= 10) tmp[++len] = std::floor(b) - std::floor(b / 10) * 10 + 48; while(len) pc(tmp[len--]); a ? (pc('.')) : void(); for(c *= 10; a; a--, c *= 10) pc(std::floor(c) - std::floor(c / 10) * 10 + 48); } else { flush(); std::cout << std::fixed << std::setprecision(a) << x.second; } }
inline void write(const char x) { pc(x); }
inline void write(const bool x) { pc(x ? 49 : 48); }
inline void write(char *x) { while (*x) pc(*x++); }
inline void write(const char *x) { while (*x) pc(*x++); }
inline void write(const std::string &x) { for (char c : x) pc(c); }
template<typename T, typename ...Tp> inline void write(T x, Tp ...y) { write(x), write(y...); }

int main()
{
	read(n, m, k);
	
	for(register int i = 1; i <= n; i++)
		for(register int j = 1; j <= m; j++)
			read(a[i][j]);
			
	int s = k;
	
	while(s != 0)
		route[++tot] = s, s /= 2;
		
	for(register int i = 1; i <= n; i++)
		for(register int j = 1; j <= m; j++)
			f[j][i][tot & 1] = a[i][j], g[j][i][tot & 1] = 1;
	
	for(register int k = tot - 1; k >= 1; k--)
	{
		int len = (route[k] + 1) / 2, rk = route[k], limj = m - rk + 1, cur = k & 1, pre = cur ^ 1;
		
		for(register int i = 1; i <= n; i++)
		{
			head = 1, tail = 0;
			
			for(register int j = 1; j < len; j++)
			{
				int v = a[i][j];
				
				box[v]++;
				
				while(head <= tail && a[i][dq[tail]] < v)
					tail--;
					
				dq[++tail] = j;
			}
			
			for(register int j = len; j <= m; j++)
			{
				int v = a[i][j];
				
				box[v]++;
				
				if(j > len)
					box[a[i][j - len]]--;
					
				while(head <= tail && dq[head] < j - len + 1)
					head++;
					
				while(head <= tail && a[i][dq[tail]] < v)
					tail--;
					
				dq[++tail] = j;
				
				int mx = a[i][dq[head]];
				
				suf[j - len + 1][i] = mx, count[j - len + 1][i] = box[mx];
			}
			
			for(register int j = m - len + 1; j <= m; j++)
				box[a[i][j]] = 0;
		}
				
		for(register int j = 1; j + len - 1 <= m; j++)
		{
			head = 1, tail = 0;
			
			for(register int i = 1; i < len; i++)
			{
				int v = suf[j][i];
				
				box[v] += count[j][i];
				
				while(head <= tail && suf[j][dq[tail]] < v)
					tail--;
					
				dq[++tail] = i;
			}
			
			for(register int i = len; i <= n; i++)
			{
				int v = suf[j][i];
				
				box[v] += count[j][i];
				
				if(i > len)
					box[suf[j][i - len]] -= count[j][i - len];
					
				while(head <= tail && dq[head] < i - len + 1)
					head++;
					
				while(head <= tail && suf[j][dq[tail]] < v)
					tail--;
					
				dq[++tail] = i;
				
				if(i >= rk && j <= limj)
				{
					int mx1 = suf[j][dq[head]], c1 = box[mx1];
					int mx2 = f[j][i - len][pre], c2 = g[j][i - len][pre];
					int mx3 = f[j + len][i][pre], c3 = g[j + len][i][pre];
					
					int mx = mx1, cnt = c1;
					
					if(mx2 > mx) mx = mx2, cnt = c2;
					else if(mx2 == mx) cnt += c2;
					
					if(mx3 > mx) mx = mx3, cnt = c3;
					else if(mx3 == mx) cnt += c3;
					
					f[j][i][cur] = mx, g[j][i][cur] = cnt;
				}
			}
			
			for(register int i = n - len + 1; i <= n; i++)
				box[suf[j][i]] = 0;
		}
	}
	
	for(register int i = k; i <= n; i++)
		for(register int j = 1; j + k - 1 <= m; j++)
			write(f[j][i][1], (j + k - 1 == m ? '\n' : ' '));
			
	for(register int i = k; i <= n; i++)
		for(register int j = 1; j + k - 1 <= m; j++)
			write(g[j][i][1], (j + k - 1 == m ? '\n' : ' '));
			
	flush();
	
	return 0;
}
原文