UVa 1312 Cricket Field (枚举+离散化)

题意:在w*h的图上有n个点,要求找出一个正方形面积最大,且没有点落在该正方形内部。

析:枚举所有的y坐标,去查找最大矩形,不断更新。

代码如下:

#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>

using namespace std ;
typedef long long LL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f3f;
const double eps = 1e-8;
const int maxn = 1e4 + 5;
const int dr[] = {0, 0, -1, 1};
const int dc[] = {-1, 1, 0, 0};
int n, m;
inline bool is_in(int r, int c){
    return r >= 0 && r < n && c >= 0 && c < m;
}
struct node{
    int x, y;
    bool operator < (const node &p) const{
        return x < p.x || (x == p.x && y < p.y);
    }
};
int d[maxn];
node a[maxn];
int t, x;

void solve(){
    int ans = 0, ansx, ansy;
    for(int i = 0; i < x; ++i){
        for(int j = i+1; j < x; ++j){
            int maxy = d[j], miny = d[i];
            int h = maxy - miny, w = 0, tmp = 0;
            for(int k = 0; k < t; ++k){
                if(a[k].y <= miny || a[k].y >= maxy)  continue;
                w = a[k].x - tmp;
                if(ans < min(w, h)){
                    ans = min(w, h);
                    ansx = tmp;  ansy = miny;
                }
                tmp = a[k].x;
            }

            w = m - tmp;
            if(ans < min(w, h)){
                ans = min(w, h);
                ansx = tmp;  ansy = miny;
            }
        }
    }
    printf("%d %d %d
", ansx, ansy, ans);
}

int main(){
    int T;  cin >> T;
    while(T--){
        scanf("%d %d %d", &t, &m, &n);
        for(int i = 0; i < t; ++i){  scanf("%d %d", &a[i].x, &a[i].y); d[i+1] = a[i].y; }
        d[0] = 0;  d[t+1] = n;
        sort(d, d+t+2);
        sort(a, a+t);
        x = unique(d, d+t+2) - d;
        solve();
        if(T)  printf("
");
    }
    return 0;
}


原文地址:https://www.cnblogs.com/dwtfukgv/p/5732231.html