算法设计与复杂性 Raid

总时间限制: 
1000ms 
内存限制: 
65536kB
描述

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?

输入

The first line is a integer T representing the number of test cases.
Each test case begins with an integer N (1 ≤ N ≤ 1000).
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.  

输出

For each test case output the minimum distance with precision of three decimal placed in a separate line.

样例输入
2
4
0 0
0 1
1 0
1 1
2 2
2 3
3 2
3 3
4
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
样例输出
1.414
0.000

求平面最近点对的问题。

下列这种方法复杂度是nlognlogn的,因为对y的排序是nlogn的。

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

using namespace std;

const double Max = 2000000000.0;

double getdis(int x1, int y1, int x2, int y2)
{
    return sqrt(1.0 * (x1 - x2) * (x1 - x2) + 1.0 * (y1 - y2) * (y1 - y2));
}

bool comp(const vector &a, const vector &b)
{
    return a[1] < b[1];
}

double getnearest(vector< vector > g, int b, int e)
{
    if(b == e)
        return Max;
    if(e - b == 1)
    {
        if(g[b][2] == g[e][2])
        {
            return Max;
        }
        return getdis(g[b][0], g[b][1], g[e][0], g[e][1]);
    }
    else
    {
        int m = (b + e) / 2;
        double mindis;
        double left = getnearest(g, b, m);
        double right = getnearest(g, m + 1, e);
        mindis = left < right ? left : right;
        vector< vector > temp;
        temp.push_back(g[m]);
        for(int i = m - 1; i >= b; i--)
        {
            if(g[m][0] - g[i][0] > mindis)
                break;
            temp.push_back(g[i]);
        }
        for(int i = m + 1; i <= e; i++)
        {
            if(g[i][0] - g[m][0] > mindis)
                break;
            temp.push_back(g[i]);
        }
        sort(temp.begin(), temp.end(), comp);
        for(int i = 0; i < temp.size(); i++)
        {
            int y = temp[i][1];
            for(int j = i + 1; j < temp.size() && mindis > temp[j][1] - y ; j++)
            {
                if(temp[i][2] == temp[j][2])
                    continue;
                double dis =getdis(temp[i][0], temp[i][1], temp[j][0], temp[j][1]);
                if(dis < mindis)
                {
                    mindis = dis;
                }
            }
        }
        return mindis;
    }
}

int main()
{
    //freopen("test.txt", "r", stdin);
    int t;
    scanf("%d", &t);
    vector< vector > graph;
    for(int i = 0; i < t; i++)
    {
        graph.clear();
        int n;
        scanf("%d", &n);
        for(int j = 0; j < n; j++)
        {
            int x, y;
            scanf("%d%d", &x, &y);
            vector a(3);
            a[0] = x;
            a[1] = y;
            a[2] = 0;
            graph.push_back(a);
        }
        for(int j = 0; j < n; j++)
        {
            int x, y;
            scanf("%d%d", &x, &y);
            vector a(3);
            a[0] = x;
            a[1] = y;
            a[2] = 1;
            graph.push_back(a);
        }
        sort(graph.begin(), graph.end());
        printf("%.3lf\n", getnearest(graph, 0, 2 * n - 1));
    }
    return 0;
}
也可以采用归并时对y进行排序,这样子复杂度变为nlogn的

#include 
#include 
#include 
#include 
#include 
#include 

using namespace std;

int n;
int t;
const double MAX = 2000000000.0;

typedef struct position
{
    int x, y;
    int type;
    position(int a, int b, int c)
    {
        x = a;
        y = b;
        type = c;
    }
}pos;

vector p;

bool comp(pos a, pos b)
{
    if(a.x < b.x)
        return true;
    if(a.x == b.x)
        if(a.y < b.y)
            return true;
        else
            return false;
    return false;
}

double getdis(int a, int b)
{
    double dis = sqrt(1.0 * (p[a].x - p[b].x) * (p[a].x - p[b].x) + 1.0 * (p[a].y - p[b].y) * (p[a].y - p[b].y));
    //printf("%lf\n", dis);
    return dis;
}

double getnear(int low, int high)
{
    if(low == high)
        return MAX;
    if(low == high - 1)
    {
        if(p[low].y > p[high].y)
        {
            pos t = p[low];
            p[low] = p[high];
            p[high] = t;
        }
        if(p[low].type == p[high].type)
        {
            return MAX;
        }
        else
        {
            return getdis(low, high);
        }
    }
    int mid = (low + high) / 2;
    double near = min(getnear(low, mid), getnear(mid + 1, high));
    vector temp;
    int i, j;
    for(i = low, j = mid + 1; i <= mid || j <= high; i++)
    {
        for(; j <= high && (i > mid || p[j].y < p[i].y); j++)
            temp.push_back(p[j]);
        if(i <= mid && abs(p[i].x - p[mid].x) < near)
        {
            for(int k = j - 1; k > mid && j - k < 3; k--)
                if(p[i]. type != p[k].type)
                    near = min(near, getdis(i, k));
            for(int k = j; k <= high && k - j < 3; k++)
                if(p[i]. type != p[k].type)
                    near = min(near, getdis(i, k));
        }
        if(i <= mid)
            temp.push_back(p[i]);
    }
    for(int i = 0; i < temp.size(); i++)
    {
        p[i + low] = temp[i];
    }
    return near;
}

int main()
{
    scanf("%d", &t);
    while(t--)
    {
        scanf("%d", &n);
        int x, y;
        p.clear();
        for(int i = 0; i < n; i++)
        {
            scanf("%d%d", &x, &y);
            p.push_back(pos(x, y, 1));
        }
        for(int i = 0; i < n; i++)
        {
            scanf("%d%d", &x, &y);
            p.push_back(pos(x, y, 2));
        }
        sort(p.begin(), p.end(), comp);
        printf("%.3lf\n", getnear(0, 2 * n - 1));
    }
    return 0;
}



你可能感兴趣的:(刷题记录,算法设计与复杂性理论)