답안 #52709

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
52709 2018-06-26T12:02:49 Z Kubalionzzale 원 고르기 (APIO18_circle_selection) C++14
19 / 100
2788 ms 68656 KB
#include <iostream>
#include <set>
#include <math.h>
#include <algorithm>
#include <functional>
#include <iterator>

#define int long long int

long double abss(long double x)
{
    if (x < 0)
        return -x;
    else
        return x;
}

long double sqr(long double x)
{
    return x * x;
}

struct compare {
    bool operator() (const std::pair<int, int>& lhs, const std::pair<int, int>& rhs) const {
        if (lhs.first != rhs.first)
            return lhs.first > rhs.first;
        else
            return lhs.second < rhs.second;
    }
};

struct circle
{
    int x, y, r;
};

circle circles[300010];

void firstSubtask(int n)
{
    std::set< std::pair<int, int>, compare > set;
    int visited[300010] = { 0 };
    int x[300010], y[300010], r[300010];
    for (int i = 1; i <= n; ++i)
    {
        std::cin >> x[i] >> y[i] >> r[i];
        set.insert(std::make_pair(r[i], i));
    }

    while (set.empty() == false)
    {
        std::pair<int, int> p = *set.begin();
        set.erase(set.begin());
        int xs = x[p.second], ys = y[p.second], rs = r[p.second];
        visited[p.second] = p.second;

        for (auto it = set.begin(); it != set.end();)
        {
            std::pair<int, int> sec = *it;
            int xq = x[sec.second], yq = y[sec.second], rq = r[sec.second];
            long double dist = sqrt(sqr(xq * 1.0 - xs * 1.0) + sqr(yq * 1.0 - ys * 1.0));

  //          std::cout << p.second << " " << sec.second << " " << dist << " " << rq * 1.0 + rs * 1.0 << "\n";

            if (dist <= rq * 1.0 + rs * 1.0)
            {
                visited[sec.second] = p.second;
                it = set.erase(it);
            }
            else
            {
                ++it;
            }
        }
    }

    for (int i = 1; i <= n; ++i)
    {
        std::cout << visited[i] << " ";
    }
}

void secondSubtask(int n)
{
    std::set< std::pair<int, int>, compare > radiuses;
    std::set< std::pair<int, int> > right, left;
    for (int i = 1; i <= n; ++i)
    {
        radiuses.insert(std::make_pair(circles[i].r, i));
        right.insert(std::make_pair(circles[i].x + circles[i].r, i));
        left.insert(std::make_pair(circles[i].x - circles[i].r, i));
    }

    int visited[300010] = { 0 };
    while (radiuses.empty() == false)
    {
        auto radius = radiuses.begin();
        std::pair<int, int> boss = *radius;
        radiuses.erase(radius);

        visited[boss.second] = boss.second;

        auto it = right.find(std::make_pair(circles[boss.second].x + circles[boss.second].r, boss.second));
        int leftCheck = circles[boss.second].x - circles[boss.second].r;
        int rightCheck = circles[boss.second].x + circles[boss.second].r;

        while (it != right.begin())
        {
            --it;
            std::pair<int, int> under = *it;

    //        std::cout << boss.second << " " << boss.first << " " << under.first << " " << under.second << " " << leftCheck << "\n";
            if (under.first >= leftCheck)
            {
                it = right.erase(it);
                left.erase(std::make_pair(circles[under.second].x - circles[under.second].r, under.second));
                visited[under.second] = boss.second;
                radiuses.erase(std::make_pair(circles[under.second].r, under.second));
            }
            else
                break;
        }

        right.erase(std::make_pair(circles[boss.second].x + circles[boss.second].r, boss.second));
        it = left.find(std::make_pair(circles[boss.second].x - circles[boss.second].r, boss.second));
        ++it;

        while (it != left.end())
        {
            std::pair<int, int> under = *it;

  //          std::cout << boss.second << " " << boss.first << " " << under.first << " " << under.second << " " << rightCheck << "\n";
            if (under.first <= rightCheck)
            {
                it = left.erase(it);
                right.erase(std::make_pair(circles[under.second].x + circles[under.second].r, under.second));
                visited[under.second] = boss.second;
                radiuses.erase(std::make_pair(circles[under.second].r, under.second));
            }
            else
                break;
        }

        left.erase(left.find(std::make_pair(circles[boss.second].x - circles[boss.second].r, boss.second)));
    }


    for (int i = 1; i <= n; ++i)
    {
        std::cout << visited[i] << " ";
    }
}

void thirdSubtask(int n)
{
    std::set< std::pair<int, int>, compare > radiuses;
    std::set< std::pair<int, int> > xses, yses;

    int visited[300010] = { 0 };
    for (int i = 1; i <= n; ++i)
    {
        radiuses.insert(std::make_pair(circles[i].r, i));
        xses.insert(std::make_pair(circles[i].x, i));
        yses.insert(std::make_pair(circles[i].y, i));
    }


    while (radiuses.empty() == false)
    {
        auto radius = radiuses.begin();
        std::pair<int, int> boss = *radius;
        radiuses.erase(radius);

        visited[boss.second] = boss.second;

        auto it = xses.find(std::make_pair(circles[boss.second].x, boss.second));
        if (it != xses.begin())
        {
            --it;
            std::pair<int, int> next = *it;
            long double dist = sqrt(sqr(circles[boss.second].x - circles[next.second].x) + sqr(circles[boss.second].y - circles[next.second].y));

            if (dist <= circles[boss.second].r + circles[next.second].r)
            {
                visited[next.second] = boss.second;
                radiuses.erase(std::make_pair(circles[next.second].r, next.second));
                xses.erase(it);
                yses.erase(std::make_pair(circles[next.second].y, next.second));
            }
        }

        it = xses.find(std::make_pair(circles[boss.second].x, boss.second));
        if (it != xses.end())
        {
            ++it;
            std::pair<int, int> next = *it;
            long double dist = sqrt(sqr(circles[boss.second].x - circles[next.second].x) + sqr(circles[boss.second].y - circles[next.second].y));

            if (dist <= circles[boss.second].r + circles[next.second].r)
            {
                visited[next.second] = boss.second;
                radiuses.erase(std::make_pair(circles[next.second].r, next.second));
                xses.erase(it);
                yses.erase(std::make_pair(circles[next.second].y, next.second));
            }
        }
        xses.erase(std::make_pair(circles[boss.second].x, boss.second));

        it = yses.find(std::make_pair(circles[boss.second].y, boss.second));
        if (it != yses.begin())
        {
            --it;
            std::pair<int, int> next = *it;
            long double dist = sqrt(sqr(circles[boss.second].x - circles[next.second].x) + sqr(circles[boss.second].y - circles[next.second].y));

            if (dist <= circles[boss.second].r + circles[next.second].r)
            {
                visited[next.second] = boss.second;
                radiuses.erase(std::make_pair(circles[next.second].r, next.second));
                yses.erase(it);
                xses.erase(std::make_pair(circles[next.second].x, next.second));
            }
        }

        it = yses.find(std::make_pair(circles[boss.second].y, boss.second));
        if (it != yses.end())
        {
            ++it;
            std::pair<int, int> next = *it;
            long double dist = sqrt(sqr(circles[boss.second].x - circles[next.second].x) + sqr(circles[boss.second].y - circles[next.second].y));

            if (dist <= circles[boss.second].r + circles[next.second].r)
            {
                visited[next.second] = boss.second;
                radiuses.erase(std::make_pair(circles[next.second].r, next.second));
                yses.erase(it);
                xses.erase(std::make_pair(circles[next.second].x, next.second));
            }
        }

        yses.erase(std::make_pair(circles[boss.second].y, boss.second));
    }

    for (int i = 1; i <= n; ++i)
    {
        std::cout << visited[i] << " ";
    }
}

main()
{
    int n;
    std::cin >> n;

    if (n <= 5000)
    {
        firstSubtask(n);
        return 0;
    }

    bool flag = true;
    for (int i = 1; i <= n; ++i)
    {
        std::cin >> circles[i].x >> circles[i].y >> circles[i].r;

        if (circles[i].y != 0)
        {
            flag = false;
        }
    }

    if (flag)
    {
        secondSubtask(n);
        return 0;
    }

    thirdSubtask(n);
}

Compilation message

circle_selection.cpp:250:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
 main()
      ^
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 2680 KB Output is correct
2 Correct 4 ms 2792 KB Output is correct
3 Correct 4 ms 2792 KB Output is correct
4 Correct 4 ms 2928 KB Output is correct
5 Correct 3 ms 2928 KB Output is correct
6 Correct 5 ms 2928 KB Output is correct
7 Correct 4 ms 3008 KB Output is correct
8 Correct 5 ms 3008 KB Output is correct
9 Correct 4 ms 3008 KB Output is correct
10 Correct 4 ms 3008 KB Output is correct
11 Correct 4 ms 3008 KB Output is correct
12 Correct 4 ms 3008 KB Output is correct
13 Correct 4 ms 3008 KB Output is correct
14 Correct 5 ms 3084 KB Output is correct
15 Correct 5 ms 3084 KB Output is correct
16 Correct 6 ms 3084 KB Output is correct
17 Correct 5 ms 3084 KB Output is correct
18 Correct 5 ms 3084 KB Output is correct
19 Correct 13 ms 3408 KB Output is correct
20 Correct 13 ms 3408 KB Output is correct
21 Correct 14 ms 3468 KB Output is correct
22 Correct 339 ms 3484 KB Output is correct
23 Correct 344 ms 3484 KB Output is correct
24 Correct 335 ms 3484 KB Output is correct
25 Correct 387 ms 3484 KB Output is correct
26 Correct 346 ms 3484 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2198 ms 68456 KB Output is correct
2 Correct 2597 ms 68656 KB Output is correct
3 Correct 2540 ms 68656 KB Output is correct
4 Correct 2788 ms 68656 KB Output is correct
5 Correct 1811 ms 68656 KB Output is correct
6 Correct 1783 ms 68656 KB Output is correct
7 Correct 1724 ms 68656 KB Output is correct
8 Correct 1770 ms 68656 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 68656 KB Output is correct
2 Incorrect 678 ms 68656 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 2166 ms 68656 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 2680 KB Output is correct
2 Correct 4 ms 2792 KB Output is correct
3 Correct 4 ms 2792 KB Output is correct
4 Correct 4 ms 2928 KB Output is correct
5 Correct 3 ms 2928 KB Output is correct
6 Correct 5 ms 2928 KB Output is correct
7 Correct 4 ms 3008 KB Output is correct
8 Correct 5 ms 3008 KB Output is correct
9 Correct 4 ms 3008 KB Output is correct
10 Correct 4 ms 3008 KB Output is correct
11 Correct 4 ms 3008 KB Output is correct
12 Correct 4 ms 3008 KB Output is correct
13 Correct 4 ms 3008 KB Output is correct
14 Correct 5 ms 3084 KB Output is correct
15 Correct 5 ms 3084 KB Output is correct
16 Correct 6 ms 3084 KB Output is correct
17 Correct 5 ms 3084 KB Output is correct
18 Correct 5 ms 3084 KB Output is correct
19 Correct 13 ms 3408 KB Output is correct
20 Correct 13 ms 3408 KB Output is correct
21 Correct 14 ms 3468 KB Output is correct
22 Correct 339 ms 3484 KB Output is correct
23 Correct 344 ms 3484 KB Output is correct
24 Correct 335 ms 3484 KB Output is correct
25 Correct 387 ms 3484 KB Output is correct
26 Correct 346 ms 3484 KB Output is correct
27 Incorrect 40 ms 68656 KB Output isn't correct
28 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 2680 KB Output is correct
2 Correct 4 ms 2792 KB Output is correct
3 Correct 4 ms 2792 KB Output is correct
4 Correct 4 ms 2928 KB Output is correct
5 Correct 3 ms 2928 KB Output is correct
6 Correct 5 ms 2928 KB Output is correct
7 Correct 4 ms 3008 KB Output is correct
8 Correct 5 ms 3008 KB Output is correct
9 Correct 4 ms 3008 KB Output is correct
10 Correct 4 ms 3008 KB Output is correct
11 Correct 4 ms 3008 KB Output is correct
12 Correct 4 ms 3008 KB Output is correct
13 Correct 4 ms 3008 KB Output is correct
14 Correct 5 ms 3084 KB Output is correct
15 Correct 5 ms 3084 KB Output is correct
16 Correct 6 ms 3084 KB Output is correct
17 Correct 5 ms 3084 KB Output is correct
18 Correct 5 ms 3084 KB Output is correct
19 Correct 13 ms 3408 KB Output is correct
20 Correct 13 ms 3408 KB Output is correct
21 Correct 14 ms 3468 KB Output is correct
22 Correct 339 ms 3484 KB Output is correct
23 Correct 344 ms 3484 KB Output is correct
24 Correct 335 ms 3484 KB Output is correct
25 Correct 387 ms 3484 KB Output is correct
26 Correct 346 ms 3484 KB Output is correct
27 Correct 2198 ms 68456 KB Output is correct
28 Correct 2597 ms 68656 KB Output is correct
29 Correct 2540 ms 68656 KB Output is correct
30 Correct 2788 ms 68656 KB Output is correct
31 Correct 1811 ms 68656 KB Output is correct
32 Correct 1783 ms 68656 KB Output is correct
33 Correct 1724 ms 68656 KB Output is correct
34 Correct 1770 ms 68656 KB Output is correct
35 Correct 4 ms 68656 KB Output is correct
36 Incorrect 678 ms 68656 KB Output isn't correct
37 Halted 0 ms 0 KB -