UVa - 1312 - Cricket Field
2015-06-27 11:09
567 查看
Once upon a time there was a greedy King who ordered his chief Architect to build a field for royal cricket inside his park. The King was so greedy, that he would not listen to his Architect's proposals to build
a field right in the park center with pleasant patterns of trees specially planted around and beautiful walks inside tree alleys for spectators. Instead, he ordered neither to cut nor to plant even a single tree in his park, but demanded to build the largest
possible cricket field for his pleasure. If the Kind finds that the Architect has dared to touch even a single tree in his park or designed a smaller field that it was possible, then the Architect will loose his head. Moreover, he demanded his Architect to
introduce at once a plan of the field with its exact location and size.
Your task is to help poor Architect to save his head, by writing a program that will find the maximum possible size of the cricket field and its location inside the park to satisfy King's requirements.
The task is somewhat simplified by the fact, that King's park has a rectangular shape and is situated on a flat ground. Moreover, park's borders are perfectly aligned with North-South and East-West lines. At the
same time, royal cricket is always played on a square field that is also aligned with North-South and East-West lines. Architect has already established a Cartesian coordinate system and has precisely measured the coordinates of every tree. This coordinate
system is, of course, aligned with North-South and East-West lines. Southwestern corner of the park has coordinates (0, 0) and Northeastern corner of the part has coordinates (W, H), where W and H are
the park width and height in feet respectively.
For this task, you may neglect the diameter of the trees. Trees cannot be inside the cricket field, but may be situated on its side. The cricket field may also touch park's border, but shall not lie outside the
park.
Input
The input begins with a single positive integer on a line by itself indicating the number of the cases following, each of them as described below. This line is followed by a blank line, and there is alsoa blank line between two consecutive inputs.
The first line of the input file contains three integer numbers N, W, and H, separated by spaces. N ( 0
N
100)
is the number of trees in the park. W and H ( 1
W, H
10000)
are the park width and height in feet respectively.
Next N lines describe coordinates of trees in the park. Each line contains two integer numbers Xi and Yiseparated
by a space ( 0
Xi
W, 0
Yi
H)
that represent coordinates of i-th tree. All trees are located at different coordinates.
Output
For each test case, the output must follow the description below. The outputs of two consecutive cases will be separated by a blank line.Write to the output file a single line with three integer numbers P, Q, and L separated by spaces, where (P,Q) are
coordinates of the cricket field Southwestern corner, and L is a length of its sides. If there are multiple possible field locations with a maximum size, then output any one.
Sample Input
1 7 10 7 3 2 4 2 7 0 7 3 4 5 2 4 1 7
Sample Output
4 3 4
按横坐标排序,然后枚举两个点,判断能构成的最大正方形。然后纵坐标再来一次。注意初始化的时候可以给四个顶点也放上树,方便处理边界。
AC代码:
#include <iostream> #include <cstdio> #include <cstdlib> #include <cctype> #include <cstring> #include <string> #include <sstream> #include <vector> #include <set> #include <map> #include <algorithm> #include <stack> #include <queue> #include <bitset> #include <cassert> #include <cmath> #include <functional> using namespace std; struct Pt { int x, y; Pt(int a = 0, int b = 0) : x(a), y(b) {} }tree[110]; int n, w, h; int P, Q, L; // 按横坐标排序用 bool cmpx(Pt a, Pt b) { if (a.x != b.x) { return a.x < b.x; } return a.y < b.y; } // 按纵坐标排序用 bool cmpy(Pt a, Pt b) { if (a.y != b.y) { return a.y < b.y; } return a.x < b.x; } void solve() { P = Q = L = 0; // 横向枚举 sort(tree, tree + n, cmpx); for (int i = 0; i < n; i++) { int mny = 0, mxy = h; for (int j = i + 1; j < n; j++) { int l = min(tree[j].x - tree[i].x, mxy - mny); // 可以构成的最大正方形变长 if (l > L) { P = tree[i].x; Q = mny; L = l; } if (tree[j].x == tree[i].x) { // 在同一列失败 continue; } if (tree[j].y > tree[i].y) { mxy = min(mxy, tree[j].y); } else { mny = max(mny, tree[i].y); } } } // 纵向枚举 sort(tree, tree + n, cmpy); for (int i = 0; i < n; i++) { int mnx = 0, mxx = w; for (int j = i + 1; j < n; j++) { int l = min(tree[j].y - tree[i].y, mxx - mnx); if (l > L) { P = mnx; Q = tree[i].y; L = l; } if (tree[j].y == tree[i].y) { continue; } if (tree[j].x > tree[i].x) { mxx = min(mxx, tree[j].x); } else { mnx = max(mnx, tree[j].x); } } } } int main() { ios::sync_with_stdio(false); int T; cin >> T; int kase = 0; while (T--) { cin >> n >> w >> h; for (int i = 0; i < n; i++) { cin >> tree[i].x >> tree[i].y; } // 四个边界也加上树 tree[n++] = Pt(0, 0); tree[n++] = Pt(w, 0); tree[n++] = Pt(0, h); tree[n++] = Pt(w, h); solve(); if (kase++) { cout << endl; } cout << P << ' ' << Q << ' ' << L << endl; } return 0; }
相关文章推荐
- Linux系统库函数 -- lseek函数用法
- 树莓派学习笔记—— 源代码方式安装opencv
- Qt 中彩色图像转换为灰度图
- 排序算法实践
- JavaScript 运行机制详解:再谈Event Loop
- Linux虚拟地址->物理地址
- Qt 中彩色图像转换为灰度图
- 教程:在ZYBO板卡实现bin文件的固化
- 树莓派2B的GPIO管脚分布
- Java数据类型和MySql数据类型对应表
- BIEE Mobile App 集成应用详细步骤说明
- 计算机类产品评价技术指标体系研究之图形(像)篇
- Java 中的 static 使用之静态初始化块
- opencv 数字图像处理-图像缩放 自己写cvResize()函数
- '-[__NSCFNumber rangeOfCharacterFromSet:]: unrecognized selector sent to instance 0xb000000000000153
- 运行和控制Nginx
- java网络编程实现客户端连接服务器端,并发送消息例子(1)。(阻塞式的方法)
- hdoj 1045 Fire Net 【DFS n皇后变形】 【二分图】
- Python socket编程(TCP)
- 5. Laravel5学习笔记:在packagist上发布自己的composer包