跳到正文

题解

题解:P13219 [GCJ 2015 #1B] Noisy Neighbors

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

文章信息

发布
2025年7月16日
收录
2025年12月12日
更新
2026年11月15日
篇幅
约 3 分钟
题目
P13219

版本

  1. v1当前

在我看来这是最水得蓝题,没有之一。

观察数据

发现 R∗CR*C 只有 10000,自然可以想到O(TRC)的做法

分析题目

自己手模样例后可以发现最优方法应该是黑白染色分开放,类似于国际象棋的棋盘。
然后分别求黑的和白的个数取min。

上代码

C++
#include <bits/stdc++.h>
using namespace std;
#define endl "\n";
#define int long long

int t, n, m, s, a[10005], b[10005], ca, cb, ans1, ans2;

void solve(int pos) {
	ca = 0, cb = 0, ans1 = 0, ans2 = 0;
	memset(a, 0, sizeof a);
	memset(b, 0, sizeof b);
	//清空
	cin >> n >> m >> s;
	for (int i = 1; i <= n; i++)
		for (int j = 1; j <= m; j++) {
			int cnt = 4;
			if (i == 1)
				cnt--;
			if (i == n)
				cnt--;
			if (j == 1)
				cnt--;
			if (j == m)
				cnt--;
			//cnt统计边数
			if (i % 2 && !(j % 2))
				a[++ca] = cnt;
			if (i % 2 && j % 2)
				b[++cb] = cnt;
			if (!(i % 2) && j % 2)
				a[++ca] = cnt;
			if (!(i % 2) && !(j % 2))
				b[++cb] = cnt;
			//可以理解为a是黑,b是白
		}
	if (ca >= s || cb >= s) {
		cout << "Case #" << pos << ": 0" << endl;
		return;
	}
	//特判,直接输出0
	sort(a + 1, a + ca + 1);
	sort(b + 1, b + cb + 1);
	for (int i = 1; i <= s - ca; i++)
		ans1 += b[i];
	for (int i = 1; i <= s - cb; i++)
		ans2 += a[i];
	//排序完分别求和
	cout << "Case #" << pos << ": " << min(ans1, ans2) << endl;
	//取min
}

signed main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin >> t;
    for (int i = 1; i <= t; i++)
    	solve(i);
    return 0;
}
原文