列表

详情


NC237148. Fortress

描述

There is a a grid with n rows and m columns, in which there are k monsters.

The i th monster lives in (x_i, y_i) (the x_i th row of the y_i th column).

Now we need to build a fortress. The fortress is a rectangular area, with the upper left corner in (r_1, c_1) and the lower right corner in (r_2, c_2).

All the rows between r_1 and r_2 and the columns between c_1 and c_2 belong to the fortress.

A valid fortress requires

After building the fortress, all monsters i satisfy or will be eliminated (i.e. the attack area of the fortress is a cross area)

Now it is required to eliminate all monsters and find the minimum area of the fortress, that is, minimize

输入描述

The first line contains 3 intergers 
- the number of rows, columns and mosters.

Each of the next k lines contains two intergers - the position of the i th monster.

输出描述

For each test case, print a single integer, the minimum area.

示例1

输入:

4 4 8
1 2
1 3
2 1
2 4
3 1
3 4
4 2
4 3

输出:

4

说明:

One of the optimal solutions of the sample test:r_1=2,r_2=3,c_1=2,c_2=3


原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

C++ 解法, 执行用时: 59ms, 内存消耗: 3340K, 提交时间: 2022-06-04 18:53:11

#include <cstdio>
#include <algorithm>

using namespace std;

typedef long long LL;

const LL N = 100010;

struct Data {
	LL x, y;
} ;

Data s[N];
LL mn[N], mx[N];

bool cmpu(Data x, Data y) {
	return x.x < y.x;
}

bool cmpd(Data x, Data y) {
	return x.x > y.x;
}

LL work(LL k) {
	
	mn[1] = mx[1] = s[1].y;
	for (LL i = 2; i <= k; ++i) {
		mn[i] = min(mn[i - 1], s[i].y);
		mx[i] = max(mx[i - 1], s[i].y);
	}
	LL l = s[k].x, r = s[k].x;
	LL res = mx[k - 1] - mn[k - 1] + 1;
	for (LL i = k - 1; i > 1; --i)
	{
		l = min(l, s[i].x);
		r = max(r, s[i].x);
		res = min(res, (r - l + 1) * (mx[i - 1] - mn[i - 1] + 1));
	}
	return res;
}

LL solve(LL k) {
	sort(s + 1, s + k + 1, cmpu);
	LL res = work(k);
	sort(s + 1, s + k + 1, cmpd);
	return min(res, work(k));
}

int main() {
	LL n, m, k;
	scanf("%lld %lld %lld", &n, &m, &k);
	for (LL i = 1; i <= k; ++i)
		scanf("%lld %lld", &s[i].x, &s[i].y);
	if (k == 1) {
		puts("1");
		return 0;
	}

	LL res = solve(k);
	swap(n, m);
	for (LL i = 1; i <= k; ++i)
		swap(s[i].x, s[i].y);
		
	printf("%lld\n", min(res, solve(k)));

	return 0;
}

上一题