P3073 [USACO13FEB] Tractor S 题解 二分+DFS

[USACO13FEB] Tractor S

传送门

题面翻译

题目描述

FJ有块农田太崎岖了,他要买一辆新拖拉机才能在这里巡视。这块农田由N x N个格子的非负整数表示高度(1<=N<=500)。拖拉机从当前格子走到相邻格子(东、南、西、北四个方向)的代价为高度差D,则FJ驶过这两个格子的拖拉机最少也要值D块钱。

FJ愿意花足够的钱买一辆新的拖拉机使得他能以最小的高度差走遍所有格子的一半(如果格子总数是奇数,那么一半的值为四舍五入的值)。因为FJ很懒,所以他找到你帮他编程计算他最小需要花多少钱买到符合这些要求的拖拉机。

输入输出格式

输入格式:

第一行为一个整数N

第2到N+1行每行包含N个非负整数(不超过1,000,000),表示当前格子的高度。

输出格式:

共一行,表示FJ买拖拉机要花的最小价钱。

题目描述

One of Farmer John’s fields is particularly hilly, and he wants to purchase a new tractor to drive around on it. The field is described by an N x N grid of non-negative integer elevations (1 <= N <= 500). A tractor capable of moving from one grid cell to an adjacent cell (one step north, east, south, or west) of height difference D costs exactly D units of money.

FJ would like to pay enough for his tractor so that, starting from some grid cell in his field, he can successfully drive the tractor around to visit at least half the grid cells in the field (if the number of total cells in the field is odd, he wants to visit at least half the cells rounded up). Please help him compute the minimum cost necessary for buying a tractor capable of this task.

输入格式

* Line 1: The value of N.

* Lines 2…1+N: Each line contains N space-separated non-negative integers (each at most 1 million) specifying a row of FJ’s field.

输出格式

* Line 1: The minimum cost of a tractor that is capable of driving around at least half of FJ’s field.

样例 #1

样例输入 #1

5 
0 0 0 3 3 
0 0 0 0 3 
0 9 9 3 3 
9 9 9 3 3 
9 9 9 9 3

样例输出 #1

3

注明

以上来自 L u o g u 以上来自Luogu 以上来自Luogu
不得不说,你谷一键复制 MarkDown 这个功能不错,但题目翻译格式依旧是()()(<-你猜我想说什么?)。

解题思路

简单题,二分答案,记录答案为 A n s w e r Answer Answer(也就是 FJ 最少要花费的价钱,即使区域内最大高度差为 A n s w e r Answer Answer,且区域大小 S ′ S' S 满足 S ′ ≥ ⌈ N × N ⌉ S' \ge\lceil N\times N\rceil SN×N)。
对于检验答案是否正确,我们使用 D F S DFS DFS。用两个数组分别记录以题目规则可以走过的区域的面积与区域的染色情况。最后检验是否有区域满足题目条件,若是,则更新答案。

AC Code

#include <bits/stdc++.h>
using namespace std;
char buf[1048576], *p1, *p2;
template<typename T>inline void Super_Quick_Read(T &x) {
	bool f = 1;
	x = 0;
	char ch = (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 20, stdin), p1 == p2) ? 0 : *p1++);
	while (ch < '0' || ch > '9') {
		if (ch == '-') f = !f;
		ch = (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 20, stdin), p1 == p2) ? 0 : *p1++);
	}
	while (ch >= '0' && ch <= '9')x = (x << 1) + (x << 3) + (ch ^ 48), ch = (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 20, stdin), p1 == p2) ? 0 : *p1++);
	x = (f ? x : -x);
	return;
}
template<typename T>inline void Quick_Write(T x) {
	if (x < 0) putchar('-'), x = -x;
	if (x > 9) Quick_Write(x / 10);
	putchar(x % 10 + '0');
	return;
}
const int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};
int N, Map[505][505];
int Flag[505][505], S[255025], Count_S_Color;
inline void Dfs(int now_x, int now_y, int mid) {
	Flag[now_x][now_y] = Count_S_Color, ++S[Count_S_Color];
	for (register int i = 0; i < 4; i++) {
		int x = now_x + dx[i], y = now_y + dy[i];
		if ((x < 0 || x >= N || y < 0 || y >= N) || Flag[x][y] || abs(Map[now_x][now_y] - Map[x][y]) > mid) continue;
		Dfs(x, y, mid);
	}
}
inline bool Check(int mid) {
	Count_S_Color = 0, memset(Flag, 0, sizeof(Flag)), memset(S, 0, sizeof(S));
	for (register int i = 0; i < N; ++i) for (register int j = 0; j < N; ++j) if (!Flag[i][j]) ++Count_S_Color, Dfs(i, j, mid);
	for (register int i = 1; i <= Count_S_Color; ++i) if (S[i] >= N * N / 2) return 1;
	return 0;
}
int main() {
	Super_Quick_Read(N);
	for (register int i = 0; i < N; ++i) for (register int j = 0; j < N; ++j) Super_Quick_Read(Map[i][j]);
	int l = 0, r = 1000005;
	int Answer = 0;
	while (l <= r) {
		int mid = (l + r) >> 1;
		if (Check(mid)) r = mid - 1, Answer = mid;
		else l = mid + 1;
	}
	Quick_Write(Answer);
	return 0;
}

然后对于此题,洛谷给了 普及 + / 提高 \color{green} 普及+/提高 普及+/提高,对此我给出一个?

  • 38
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值