UVA - 1312 - Cricket Field(离散化)
题意:给定h * w(1 <= h, w <= 10^4)的区域,区域内的坐标范围为0~h和0~w,然后给定区域内n个点(0 <= n <= 100),求在区域内一个不包含点的最大面积的正方形(但点可以在正方形边界上),输出左下角坐标及边长(多解则任意输出)。
1、找出在h方向的所有边界;
2、二重循环枚举任意两个边界;
3、枚举范围内的点,构造矩形,对于每个矩形来说,长和宽其中较小的一个即为正方形的边长。
以上不会丢失最优解,即使有的矩形在h方向上可以扩展,但是枚举了任意h方向的两个边界,一定会取到。
以下是图示:
#include<cstdio> #include<cstring> #include<cctype> #include<cstdlib> #include<cmath> #include<iostream> #include<sstream> #include<iterator> #include<algorithm> #include<string> #include<vector> #include<set> #include<map> #include<deque> #include<queue> #include<stack> #include<list> #define fin freopen("in.txt", "r", stdin) #define fout freopen("out.txt", "w", stdout) #define pr(x) cout << #x << " : " << x << " " #define prln(x) cout << #x << " : " << x << endl #define Min(a, b) a < b ? a : b #define Max(a, b) a < b ? b : a typedef long long ll; typedef unsigned long long llu; const int INT_INF = 0x3f3f3f3f; const int INT_M_INF = 0x7f7f7f7f; const ll LL_INF = 0x3f3f3f3f3f3f3f3f; const ll LL_M_INF = 0x7f7f7f7f7f7f7f7f; const double pi = acos(-1.0); const double EPS = 1e-6; const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1}; const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1}; const ll MOD = 1e9 + 7; using namespace std; #define NDEBUG #include<cassert> const int MAXN = 100 + 10; const int MAXT = 10000 + 10; int T, h, w, n, y[MAXN], m; struct Node{ int x, y; bool operator < (const Node &rhs) const{ return x < rhs.x || (x == rhs.x && y < rhs.y); } }p[MAXN]; int main(){ scanf("%d", &T); while(T--){ scanf("%d%d%d", &n, &w, &h); for(int i = 0; i < n; ++i){ scanf("%d%d", &p[i].x, &p[i].y); y[i] = p[i].y; } sort(p, p + n); y[n] = 0; y[n + 1] = h; sort(y, y + n + 2); m = unique(y, y + n + 2) - y; int ans = 0, ansx = 0, ansy = 0; for(int i = 0; i < m; ++i) for(int j = i + 1; j < m; ++j){ int miny = y[i], maxy = y[j], lur = 0; int ww = maxy - miny; for(int k = 0; k < n; ++k){ if(p[k].y >= maxy || p[k].y <= miny) continue; int hh = p[k].x - lur; int tmp = Min(ww, hh); if(tmp > ans){ ans = tmp; ansx = lur; ansy = miny; } lur = p[k].x; } int hh = w - lur; int tmp = Min(ww, hh); if(tmp > ans){ ans = tmp; ansx = lur; ansy = miny; } } printf("%d %d %d\n", ansx, ansy, ans); if(T) printf("\n"); } return 0; }