刚学 -1 天 OI,50 pts 求助
查看原帖
刚学 -1 天 OI,50 pts 求助
553245
Hansitory楼主2023/11/7 19:58
#include <bits/stdc++.h>

using namespace std;
const int N = 1010, MOD = 998244353;
int T, id, n, m;
long long a[N][N][2], c, f;
bool vis[N][N];
char s[N][N];

void calc() {
	long long ans1 = 0, ans2 = 0;
	memset(vis, 0, sizeof vis);
	for (int i = 1; i <= n - 2; i++) {
		for (int j = 1; j <= m - 1; j++) {
			int r = i;
			while (r <= n - 2 && a[r][j][1] <= 2) r += a[r][j][1] + 1;
			if (r > n - 2 || vis[r][j]) continue;
			vis[r][j] = 1;
			for (int k = r + 2; k < r + a[r][j][1]; k++) {
//				cout << r << ' ' << j << ' ' << k << endl;
				ans1 = (ans1 + (a[r][j][0] - 1) * (a[k][j][0] - 1)) % MOD;
				ans2 = (ans2 + (a[r][j][0] - 1) * (a[k][j][0] - 1) * (a[k][j][1] - 1)) % MOD;
//				cout << ans1 << ' '<< ans2 << endl; 
			}
		}
	}
	c *= ans1, f *= ans2;
}

int main() {
	scanf("%d%d", &T, &id);
	while (T--) {
		scanf("%d%d%lld%lld", &n, &m, &c, &f);
		if (c == 0 && f == 0)  { printf("0 0\n"); continue; }
		
		for (int i = 1; i <= n; i++) {
			scanf("%s", s[i] + 1);
		}
		for (int i = 1; i <= n; i++) {
			s[n + 1][i] = '1';
			s[i][m + 1] = '1';
		}
		
		memset(a, 0, sizeof a);
		for (int i = 1; i <= n + 1; i++) {
			for (int j = 1; j <= m + 1; j++) {
				if (s[i][j] != '1') continue;
				
				int now = j - 1;
				while (now > 0 && s[i][now] != '1') {
					a[i][now][0] = j - now;
					now--;
				}
				
				now = i - 1;
				while (now > 0 && s[now][j] != '1') {
					a[now][j][1] = i - now;
					now--;
				}
			}
		}
		
		calc();
		printf("%lld %lld\n", c, f);
	}
} 

评测链接

代码及评测如上。 已知时间复杂度不对,只是测试下赛场上非最优求解的得分情况。

看了下 WA 的几个点,结果误差都在个位数,应该存在边界问题,还请不吝指出,萌新感激不尽 QwQ

2023/11/7 19:58
加载中...