HDU 1007 解題報告

題目:

Have you ever played quoit in a playground? Quoit is a game in which flat rings are pitched at some toys, with all the toys encircled awarded.
In the field of Cyberground, the position of each toy is fixed, and the ring is carefully designed so it can only encircle one toy at a time. On the other hand, to make the game look more attractive, the ring is designed to have the largest radius. Given a configuration of the field, you are supposed to find the radius of such a ring.

Assume that all the toys are points on a plane. A point is encircled by the ring if the distance between the point and the center of the ring is strictly less than the radius of the ring. If two toys are placed at the same point, the radius of the ring is considered to be 0.

Input:

The input consists of several test cases. For each case, the first line contains an integer N (2 <= N <= 100,000), the total number of toys in the field. Then N lines follow, each contains a pair of (x, y) which are the coordinates of a toy. The input is terminated by N = 0.

Output:

For each test case, print in one line the radius of the ring required by the Cyberground manager, accurate up to 2 decimal places.

題目鏈接: HDU1007

題目分析:

這題就是給定N個點的座標,尋找最小點對的距離,輸出該距離併除二(得到圓環半徑)

首先容易想到的是使用兩個循環嵌套來遍歷整個圖以得到最小點對距離,可是這種算法的時間複雜度爲O(n^2),絕對會TLE。

然後想到用分治法。如下圖,將整個圖分成左右兩部分,則最小點對只可能出現在左邊、右邊、或者橫跨左右兩邊。

avatar

假設從左邊得到的最短點對距離爲MinDistLeft,從右邊得到的最短點對距離爲MinDistRight,則取兩者較小值作爲MinDist:

MinDist=min(MinDistLeft,MinDistRight);

然後考慮最小點對橫跨左右兩邊的情況。

若最小點對橫跨左右兩邊,則點對距離大於上面已給出的MinDist的點對我們就可以不予考慮了,因爲我們要的是最小點對距離。也就是說,我們要的最小點對只可能出現在x=[mid-MinDist,mid+MinDist]的範圍之內,如下圖。

avatar

而對於出現在[mid-MinDist,mid+MinDist]的最小點對,其縱座標值之差也不會超過MinDist(如果超過了就說明這不是最小點對).然後把所有符合特徵的點對求出距離與MinDist比較以得出最小點對距離。

下面是我的AC代碼,C++模式下通過:


#include <iostream>
#include <cmath>
#include <algorithm>
#include <vector>
#define INF 0xFFFFFFFF

using namespace std;
typedef struct {
    double x;
    double y;
}Node;
double dis(double ax, double ay, double bx, double by)
{
    return sqrt((ax - bx) *(ax - bx) + (ay - by) *(ay - by));
}
double dis(Node a, Node b)
{
    return dis(a.x, a.y, b.x, b.y);
}
double min(double a, double b)
{
    return a < b ? a : b;
}
double findClose(Node *nodes, int start, int end) {
    if (start == end)
        return INF;
    else if (end - start == 1)
        return dis(nodes[start], nodes[end]);

    int mid = (start + end) / 2;

    //分解問題
    double minDistLeft = findClose(nodes, start, mid - 1);
    double minDistRight = findClose(nodes, mid, end);

    double minDist = min(minDistLeft, minDistRight);
    //用於存儲跨界情況可能的點下標
    vector<int> *v = new vector<int>;

    for (int i = start; i <= end; i++)
        if (dis(nodes[i].x, 0, nodes[mid].x, 0) <= minDist)
            v->push_back(i);

    sort(v->begin(), v->end(), [nodes, v](int a, int b)->bool { //y軸升序排序
        return nodes[a].y < nodes[b].y;
    });
    for (int i = 0; i < v->size(); i++)
        for (int j = i + 1; j < v->size() && nodes[(*v)[j]].y - nodes[(*v)[i]].y < minDist; j++) //只有當符合條件的才繼續循環,一旦發現不合適的點對組合則說明下個點對也不合適
            minDist = min(minDist, dis(nodes[(*v)[j]], nodes[(*v)[i]]));                         
    delete v;
    return minDist;

}
int main()
{
    int points;
    while (cin >> points)
    {
        if (points == 0)
            break;
        Node * node = new Node[points];
        for (int i = 0; i < points; i++)
            cin >> node[i].x >> node[i].y;
        sort(node, node + points, [](Node &a, Node &b)->bool {
            if (a.x != b.x)
                return a.x < b.x;
            else
                return a.y < b.y;
        });
        printf("%.2f\n", findClose(node, 0, points - 1) / 2);
    }
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章