Description

After successive failures in the battles against the Union, the Empire retreated to its last stronghold. Depending on its powerful defense system, the Empire repelled the six waves of Union's attack. After several sleepless nights of thinking, Arthur, General of the Union, noticed that the only weakness of the defense system was its energy supply. The system was charged by N nuclear power stations and breaking down any of them would disable the system.

The general soon started a raid to the stations by N special agents who were paradroped into the stronghold. Unfortunately they failed to land at the expected positions due to the attack by the Empire Air Force. As an experienced general, Arthur soon realized that he needed to rearrange the plan. The first thing he wants to know now is that which agent is the nearest to any power station. Could you, the chief officer, help the general to calculate the minimum distance between an agent and a station?

Input

The first line is a integer T representing the number of test cases.
Each test case begins with an integer N (1 ≤ N ≤ 100000).
The next N lines describe the positions of the stations. Each line consists of two integers X (0 ≤ X ≤ 1000000000) and Y (0 ≤ Y ≤ 1000000000) indicating the positions of the station.
The next following N lines describe the positions of the agents. Each line consists of two integers X (0 ≤ X ≤ 1000000000) and Y (0 ≤ Y ≤ 1000000000) indicating the positions of the agent. 

 

题目大意:给一个点集A,一个点集B,求min(distance(x, y))(x∈A,y∈B)

思路1:分治法。把点集按x坐标从小到大排序,mid = (left + right)/2,递归分治计算出左边部分和右边部分的最小距离mind,那么,若左半部分和右半部分存在一对点距离小于mind,那么这两个点一定在范围(x[mid] -mind ,x[mid] -mind)之间(因为在这之外的点与对面的点的距离必然大于mind)

思路2:暴力枚举+剪枝。暴力枚举每两个点之间的距离,若y[j] - y[i] >= mind则break(这个不用解释了吧……)

PS:要保留3位小数,题目没说,可以看样例

 

分治算法:1141MS

 1 #include <cstdio>
 2 #include <cmath>
 3 #include <algorithm>
 4 using namespace std;
 5 
 6 const double MAX_DIST = 1e100;
 7 const int MAXN = 200010;
 8 
 9 struct Point {
10     double x, y;
11     bool flag;
12 };
13 
14 inline double dist(const Point &a, const Point &b) {
15     if(a.flag != b.flag)
16         return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
17     else
18         return MAX_DIST;
19 }
20 
21 Point pt[MAXN];
22 int y_sort[MAXN];
23 
24 inline bool x_cmp(const Point &a, const Point &b) {
25     return a.x < b.x;
26 }
27 
28 inline bool y_cmp(const int &a, const int &b) {
29     return pt[a].y < pt[b].y;
30 }
31 
32 double shortest_distance(int left, int right) {
33     if(right - left == 1)
34         return dist(pt[left], pt[right]);
35     else if(right - left == 2)
36         return min(min(dist(pt[left], pt[left+1]), dist(pt[left], pt[left+2])),
37                         dist(pt[left+1], pt[left+2]));
38     int mid = (left + right) >> 1;
39     double mind = min(shortest_distance(left, mid), shortest_distance(mid+1, right));
40     if(mind == 0) return 0;
41     int yn = 0;
42     for(int i = mid; pt[mid].x - pt[i].x < mind && i >= left; --i)
43         y_sort[yn++] = i;
44     int y_mid = yn;
45     for(int i = mid+1; pt[i].x - pt[mid].x < mind && i <= right; ++i)
46         y_sort[yn++] = i;
47     for(int i = 0; i < y_mid; ++i) for(int j = y_mid; j < yn; ++j)
48         mind = min(mind, dist(pt[y_sort[i]], pt[y_sort[j]]));
49     return mind;
50 }
51 
52 int main() {
53     int T;
54     scanf("%d", &T);
55     while(T--) {
56         int n;
57         scanf("%d", &n);
58         for(int i = 0; i < n; ++i) {
59             scanf("%lf%lf", &pt[i].x, &pt[i].y);
60             pt[i].flag = false;
61         }
62         for(int i = n; i < 2*n; ++i) {
63             scanf("%lf%lf", &pt[i].x, &pt[i].y);
64             pt[i].flag = true;
65         }
66         sort(pt, pt + 2*n, x_cmp);
67         printf("%.3f\n", shortest_distance(0, 2 * n - 1));
68     }
69 }
View Code

 分治算法:1485MS

#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;

const double MAX_DIST = 1e100;
const int MAXN = 200010;

struct Point {
    double x, y;
    bool flag;
};

inline double dist(const Point &a, const Point &b) {
    if(a.flag != b.flag)
        return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
    else
        return MAX_DIST;
}

Point pt[MAXN];
int y_sort[MAXN];

inline bool x_cmp(const Point &a, const Point &b) {
    return a.x < b.x;
}

inline bool y_cmp(const int &a, const int &b) {
    return pt[a].y < pt[b].y;
}

void _min(double &a, const double &b) {
    if(a > b) a = b;
}

double shortest_distance(int left, int right) {
    if(right - left == 1)
        return dist(pt[left], pt[right]);
    else if(right - left == 2)
        return min(min(dist(pt[left], pt[left+1]), dist(pt[left], pt[left+2])),
                        dist(pt[left+1], pt[left+2]));
    int mid = (left + right) >> 1;
    double mind = min(shortest_distance(left, mid), shortest_distance(mid+1, right));
    if(mind == 0) return 0;
    int yn = 0;
    for(int i = mid; pt[mid].x - pt[i].x < mind && i >= left; --i)
        y_sort[yn++] = i;
    for(int i = mid+1; pt[i].x - pt[mid].x < mind && i <= right; ++i)
        y_sort[yn++] = i;
    sort(y_sort, y_sort + yn);
    for(int i = 0; i < yn; ++i) {
        for(int j = i + 1; j < yn; ++j) {
            if(pt[y_sort[j]].y - pt[y_sort[i]].y >= mind) break;
            _min(mind, dist(pt[y_sort[i]], pt[y_sort[j]]));
        }
    }
    return mind;
}

int main() {
    freopen("f:/data.in", "r", stdin);
    int T;
    scanf("%d", &T);
    while(T--) {
        int n;
        scanf("%d", &n);
        for(int i = 0; i < n; ++i) {
            scanf("%lf%lf", &pt[i].x, &pt[i].y);
            pt[i].flag = false;
        }
        for(int i = n; i < 2*n; ++i) {
            scanf("%lf%lf", &pt[i].x, &pt[i].y);
            pt[i].flag = true;
        }
        sort(pt, pt + 2*n, x_cmp);
        printf("%.3f\n", shortest_distance(0, 2 * n - 1));
    }
}
View Code

相关文章: