跳到正文

题解

USACO 铜组 T2 解题报告

vanueber约 3 分钟
原文
文章信息与版本

文章信息

发布
2024年12月15日
收录
2026年1月10日
更新
2026年11月6日
篇幅
约 3 分钟
题目
P11450

版本

  1. v1当前

题目大意

在类似 mc 的反重力世界中,有一个 n×n×nn \times n \times n 大方块,每次敲掉其中的一个方块,同时询问有多少种方案能放下一个 1×1×n1 \times 1 \times n 的块。

题目分析

若是直接模拟,数组都开不下。

但是如果放在一个二维平面上考虑,问题会简单很多:

我们用 (i,j,k)(i,j,k) 表示一个从 (i,j,k)(i,j,k) 到 (i+1,j+1,k+1)(i+1,j+1,k+1) 的方块

压缩 yy 轴,用 hi,jh_{i,j} 表示(i,j,1)(i,j,1) 及其上方有多少奶酪块。

那么 (i,j,1)(i,j,1) 到 (i,j,n)(i,j,n) 能够贡献答案,当且仅当 hi,j=0h_{i,j}=0。

同理地,压缩其他坐标轴,也有类似的结论。

于是只用开三个二维数组分别记录下就行。时间复杂度 Θ(q)\Theta(q),空间复杂度 Θ(n2)\Theta(n^2)。

代码实现

C++
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <cstring>
#include <queue>
#include <ctime>
#include <random>

using namespace std;

const int INF = 0x3f3f3f3f;

inline int read()
{
	int w = 0, f = 1;
	char ch = getchar();
	while (ch < '0' || ch > '9')
	{
		if (ch == '-') f = -1;
		ch = getchar();
	}
	while (ch >= '0' && ch <= '9')
	{
		w = (w << 1) + (w << 3) + (ch ^ 48);
		ch = getchar();
	}
	return w * f;
}

inline void write(int x)
{
	if (x < 0)
	{
		putchar('-');
		x = -x;
	}
	if (x > 9) write(x / 10);
	putchar(x % 10 + '0');
}

const int maxn=1005;
int n,q;
int res1[maxn][maxn],res2[maxn][maxn],res3[maxn][maxn];
//res1 固定x,y当前剩的z
//res2 固定x,z当前剩的y
//res3 固定y,z当前剩的x

int main()
{
	n=read(),q=read();
	for(int i=1;i<=n;++i)
	{
		for(int j=1;j<=n;++j)
		{
			res1[i][j]=res2[i][j]=res3[i][j]=n;
		}
	}
	int ans=0;
	for(int i=1,x,y,z;i<=q;++i)
	{
		x=read(),y=read(),z=read();
		++x,++y,++z;
		res1[x][y]--,res2[x][z]--,res3[y][z]--;
		if(res1[x][y]==0) ++ans;
		if(res2[x][z]==0) ++ans;
		if(res3[y][z]==0) ++ans;
		cout<<ans<<endl;
	}
	return 0;
}

原文