NC237148. Fortress
描述
输入描述
The first line contains 3 intergers
- the number of rows, columns and mosters.
Each of the next lines contains two intergers - the position of the 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: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; }