24h購物| | PChome| 登入
2012-09-22 08:20:32| 人氣711| 回應0 | 上一篇 | 下一篇

[ACM-ICPC][Asia - Seoul][Farthest Pair] 4728 - Squares

推薦 0 收藏 0 轉貼0 訂閱站台

The famous Korean IT company epsfbox{p4728_c.eps} plans to make a digital map of the Earth with help of wireless sensors which spread out in rough terrains. Each sensor sends a geographical data to epsfbox{p4728_c.eps}. But, due to the inaccuracy of the sensing devices equipped in the sensors, epsfbox{p4728_c.eps} only knows a square region in which each geographical data happens. Thus a geographical data can be any point in a square region. You are asked to solve some geometric problem, known as diameter problem, on these undetermined points in the squares.

A diameter for a set of points in the plane is defined as the maximum (Euclidean) distance among pairs of the points in the set. The diameter is used as a measurement to estimate the geographical size of the set. epsfbox{p4728_c.eps} wants you to compute the largest diameter of the points chosen from the squares. In other words, given a set of squares in the plane, you have to choose exactly one point from each square so that the diameter for the chosen points is maximized. The sides of the squares are parallel to X-axis or Y-axis, and the squares may have different sizes, intersect each other, and share the same corners.

For example, if there are six squares as in the figure below, then the largest diameter is defined as the distance between two corner points of squares S1 and S4.

epsfbox{p4728.eps}

Given a set of n squares in the plane, write a program to compute the largest diameter D of the points when a point is chosen from each square, and to output D2, i.e., the squared value of D.

Input 

Your program is to read from standard input. The input consists of T test cases. The number of test cases T is given in the first line of the input. The first line of each test case contains an integer, n, the number of squares, where 2$ le$n$ le$100, 000. Each line of the next n lines contains three integers, x, y, and w, where (x, y) is the coordinate of the left-lower corner of a square and w is the length of a side of the square; 0$ le$x, y$ le$10, 000 and 1$ le$w$ le$10, 000.

Output 

Your program is to write to standard output. Print exactly one line for each test case. The line should contain the integral value D2, where D is the largest diameter of the points when a point is chosen from each square.

The following shows sample input and output for two test cases.

Sample Input 

2 
3 
0 0 1 
1 0 2 
0 0 1 
6 
2 1 2 
1 4 2 
3 2 3 
4 4 4 
6 5 1 
5 1 3

Sample Output 

13 
85

要找最遠點對, 必須做一次凸包

做法是旋轉卡尺, 不過我想我的做法不是標準的旋轉卡尺, 不過找最遠點對的時候, 仍然是 O(n)


#include <cstdio>
#include <algorithm>
using namespace std;
typedef struct {
int x, y;
} Pt;
Pt P[500000], H[500000];
bool cmp(Pt a, Pt b) {
if(a.x != b.x)
return a.x < b.x;
return a.y < b.y;
}
int cross(Pt &o, Pt &a, Pt &b) {
return (a.x-o.x)*(b.y-o.y) - (a.y-o.y)*(b.x-o.x);
}
void solve(int n) {
sort(P, P+n, cmp);
int i, j, m = 0, t;
for(i = 0; i < n; i++) {
while(m >= 2 && cross(H[m-2], H[m-1], P[i]) <= 0)
m--;
H[m++] = P[i];
}
for(i = n-1, t = m+1; i >= 0; i--) {
while(m >= t && cross(H[m-2], H[m-1], P[i]) <= 0)
m--;
H[m++] = P[i];
}
m--;
int ans = 0, tmp, now;
for(i = 0, j= 1; i < m; i++) {
while(1) {
now = (H[i].x-H[j].x)*(H[i].x-H[j].x)+
(H[i].y-H[j].y)*(H[i].y-H[j].y);
j++;
if(j >= m) j = 0;
tmp = (H[i].x-H[j].x)*(H[i].x-H[j].x)+
(H[i].y-H[j].y)*(H[i].y-H[j].y);
if(tmp < now) {
j--;
if(j < 0) j = m-1;
break;
}
}
if(now > ans) ans = now;
}
printf("%d\n", ans);
}
int main() {
int t, x, y, w;
scanf("%d", &t);
while(t--) {
int n, m = 0;
scanf("%d", &n);
while(n--) {
scanf("%d %d %d", &x, &y, &w);
P[m].x = x, P[m++].y = y;
P[m].x = x+w, P[m++].y = y;
P[m].x = x, P[m++].y = y+w;
P[m].x = x+w, P[m++].y = y+w;
}
solve(m);
}
return 0;
}
 


台長: Morris
人氣(711) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[ACM-ICPC][Asia - Seoul][殺人遊戲][模擬解] 4727 - Jump
此分類上一篇:[ACM-ICPC][Asia - Seoul] 4723 - Ducci Sequence

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文