Submission #774173

# Submission time Handle Problem Language Result Execution time Memory
774173 2023-07-05T12:32:23 Z xetrk Circle selection (APIO18_circle_selection) C++14
87 / 100
3000 ms 46844 KB
#pragma warning(disable:4996)

#include<bits/stdc++.h>
using namespace std;


typedef long long ll;
typedef long double ld;

int n;

vector<vector<ll>> circle;

vector<pair<ll, int>> sortRadius, sortX, sortY;


int ans[301000];
int parentX[300100], parentY[300100];
//int depthX[300100], depthY[300100];

inline ll GetDist(ll x1, ll y1, ll x2, ll y2)
{
    // 아 좌표값 마이너스도 들어오네
    ll xx = max(x1, x2) - min(x1, x2);
    ll yy = max(y1, y2) - min(y1, y2);
    return (xx * xx) + (yy * yy);
}



bool comp(const pair<ll, int>& a, const pair<ll, int>& b)
{
    if (a.first == b.first)
        return a.second < b.second;
    return a.first < b.first;
}

bool compSort(const pair<ll, int>& a, const pair<ll, int>& b)
{
    if (a.first == b.first)
        return a.second < b.second;

    return a.first > b.first;
}


int find(int a, int parent[])
{
    if (parent[a] != a)
        parent[a] = find(parent[a], parent);
    return parent[a];
}

void merge(int a, int b, int parent[]) 
{
    a = find(a, parent), b = find(b, parent);
    if (a > b) swap(a, b);//b가더커야함
    parent[a] = b;//더큰걸부모로
}

int main()
{
    ll x, y, r, r2;
    int i;
    scanf("%d", &n);

    for (int i = 0; i < n; i++)
    {
        scanf("%lld %lld %lld", &x, &y, &r);
        //x = 1, y = 1, r = 3;
        circle.push_back({ x,y,r });
        sortRadius.push_back({ r,i });
        sortX.push_back({ x,i });
        sortY.push_back({ y,i });
        parentX[i] = parentY[i] = i;
        //depthX[i] = depthY[i] = 1;
    }

    sort(sortRadius.begin(), sortRadius.end(), compSort);
    sort(sortX.begin(), sortX.end());
    sort(sortY.begin(), sortY.end());


    int cur, curcmp;
    int xMin, xMax, yMin, yMax;
    int next;
    ll rr;

    fill(ans, ans + n + 2, -1);

    for (int curDelete = 0; curDelete < n; curDelete++)
    {
        cur = sortRadius[curDelete].second;
        if (ans[cur] != -1)
            continue;


        x = circle[cur][0], y = circle[cur][1], r = circle[cur][2];
        if (curDelete < n - 1)
            r2 = r + sortRadius[curDelete + 1].first + 1;
        else
            r2 = r * 2 + 1;

        // a이하를 찾아야하는데 lower bound는  a이상인걸 찾으니까.. 근데 그 이하인걸 찾아야하니 1빼줌
        xMin = lower_bound(sortX.begin(), sortX.end(), pair<ll, int>(x - r2, -1), comp) - sortX.begin();
        xMax = upper_bound(sortX.begin(), sortX.end(), pair<ll, int>(x + r2, 301000), comp) - sortX.begin();

        yMin = lower_bound(sortY.begin(), sortY.end(), pair<ll, int>(y - r2, -1), comp) - sortY.begin();
        yMax = upper_bound(sortY.begin(), sortY.end(), pair<ll, int>(y + r2, 301000), comp) - sortY.begin();

        xMin--;
        yMin--;

        if (xMin < 0) xMin = 0;
        if (yMin < 0) yMin = 0;

        if (xMin >= sortX.size()) xMin = sortX.size() - 1;
        if (yMin >= sortY.size()) yMin = sortY.size() - 1;
        if (xMax >= sortX.size()) xMax = sortX.size() - 1;
        if (yMax >= sortY.size()) yMax = sortY.size() - 1;

        //xMin = 0, yMin = 0, xMax = n - 1, yMax = n - 1;


        ans[cur] = cur;
        //xIdx[cur]

        if (xMax - xMin < yMax - yMin)
        {
            i = xMin;
            while (i <= xMax)
            {
                curcmp = sortX[i].second;
                
                if (ans[curcmp] != -1)
                {
                    i = find(i, parentX) + 1;
                    continue;
                }
                rr = r + circle[curcmp][2];
                if (GetDist(x, y, circle[curcmp][0], circle[curcmp][1]) <= rr * rr)
                {
                    ans[curcmp] = cur;

                    if (i - 1 >= 0 && ans[sortX[i - 1].second] != -1)
                        merge(i, i - 1, parentX);
                    if (i + 1 < n && ans[sortX[i + 1].second] != -1)
                        merge(i, i + 1, parentX);
                   
                }
                i = find(i, parentX) + 1;
            }
        }
        else
        {
            i = yMin;
            while (i <= yMax)
            {
                curcmp = sortY[i].second;
                if (ans[curcmp] != -1)
                {
                    i = find(i, parentY) + 1;
                    continue;
                }
                rr = r + circle[curcmp][2];
                if (GetDist(x, y, circle[curcmp][0], circle[curcmp][1]) <= rr * rr)
                {
                    ans[curcmp] = cur;

                    if (i - 1 >= 0 && ans[sortY[i - 1].second] != -1)
                        merge(i, i - 1, parentY);
                    if (i + 1 < n && ans[sortY[i + 1].second] != -1)
                        merge(i, i + 1, parentY);

                }
                i = find(i, parentY) + 1;
            }
            /*
            for (int i = yMax; i >= yMin; i--)
            {
             curcmp = sortY[i].second;
             if (ans[curcmp]!= -1) continue;
             rr = r + circle[curcmp][2];
             if (GetDist(x, y, circle[curcmp][0], circle[curcmp][1]) <= rr * rr)
             {
              ans[curcmp] = cur;
              //sortY.erase(sortY.begin() + i);
             }
            }*/
        }
    }

    for (int i = 0; i < n; i++)
        printf("%d ", ans[i] + 1);

    return 0;
}

Compilation message

circle_selection.cpp:1: warning: ignoring '#pragma warning ' [-Wunknown-pragmas]
    1 | #pragma warning(disable:4996)
      | 
circle_selection.cpp: In function 'int main()':
circle_selection.cpp:117:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  117 |         if (xMin >= sortX.size()) xMin = sortX.size() - 1;
      |             ~~~~~^~~~~~~~~~~~~~~
circle_selection.cpp:118:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  118 |         if (yMin >= sortY.size()) yMin = sortY.size() - 1;
      |             ~~~~~^~~~~~~~~~~~~~~
circle_selection.cpp:119:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  119 |         if (xMax >= sortX.size()) xMax = sortX.size() - 1;
      |             ~~~~~^~~~~~~~~~~~~~~
circle_selection.cpp:120:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  120 |         if (yMax >= sortY.size()) yMax = sortY.size() - 1;
      |             ~~~~~^~~~~~~~~~~~~~~
circle_selection.cpp:86:9: warning: unused variable 'next' [-Wunused-variable]
   86 |     int next;
      |         ^~~~
circle_selection.cpp:65:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   65 |     scanf("%d", &n);
      |     ~~~~~^~~~~~~~~~
circle_selection.cpp:69:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   69 |         scanf("%lld %lld %lld", &x, &y, &r);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 304 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 304 KB Output is correct
7 Correct 1 ms 316 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 312 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 468 KB Output is correct
17 Correct 1 ms 448 KB Output is correct
18 Correct 1 ms 468 KB Output is correct
19 Correct 4 ms 1212 KB Output is correct
20 Correct 4 ms 1220 KB Output is correct
21 Correct 4 ms 1308 KB Output is correct
22 Correct 5 ms 1236 KB Output is correct
23 Correct 5 ms 1220 KB Output is correct
24 Correct 6 ms 1236 KB Output is correct
25 Correct 5 ms 1236 KB Output is correct
26 Correct 6 ms 1236 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 204 ms 36752 KB Output is correct
2 Correct 233 ms 38488 KB Output is correct
3 Correct 203 ms 36408 KB Output is correct
4 Correct 210 ms 36732 KB Output is correct
5 Correct 228 ms 36428 KB Output is correct
6 Correct 294 ms 40964 KB Output is correct
7 Correct 234 ms 41032 KB Output is correct
8 Correct 249 ms 41064 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 300 KB Output is correct
2 Correct 198 ms 12472 KB Output is correct
3 Correct 1026 ms 36592 KB Output is correct
4 Correct 1036 ms 36600 KB Output is correct
5 Correct 1427 ms 35696 KB Output is correct
6 Correct 348 ms 23340 KB Output is correct
7 Correct 110 ms 12324 KB Output is correct
8 Correct 18 ms 2928 KB Output is correct
9 Correct 1041 ms 44396 KB Output is correct
10 Correct 2324 ms 44696 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 728 ms 36560 KB Output is correct
2 Correct 554 ms 36604 KB Output is correct
3 Correct 1280 ms 36224 KB Output is correct
4 Correct 571 ms 43668 KB Output is correct
5 Correct 534 ms 43960 KB Output is correct
6 Correct 527 ms 44160 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 304 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 304 KB Output is correct
7 Correct 1 ms 316 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 312 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 468 KB Output is correct
17 Correct 1 ms 448 KB Output is correct
18 Correct 1 ms 468 KB Output is correct
19 Correct 4 ms 1212 KB Output is correct
20 Correct 4 ms 1220 KB Output is correct
21 Correct 4 ms 1308 KB Output is correct
22 Correct 5 ms 1236 KB Output is correct
23 Correct 5 ms 1220 KB Output is correct
24 Correct 6 ms 1236 KB Output is correct
25 Correct 5 ms 1236 KB Output is correct
26 Correct 6 ms 1236 KB Output is correct
27 Correct 7 ms 1928 KB Output is correct
28 Correct 7 ms 1912 KB Output is correct
29 Correct 7 ms 1844 KB Output is correct
30 Correct 11 ms 1868 KB Output is correct
31 Correct 11 ms 1804 KB Output is correct
32 Correct 11 ms 1828 KB Output is correct
33 Correct 73 ms 15292 KB Output is correct
34 Correct 70 ms 15340 KB Output is correct
35 Correct 91 ms 15116 KB Output is correct
36 Correct 145 ms 14708 KB Output is correct
37 Correct 138 ms 14796 KB Output is correct
38 Correct 179 ms 14824 KB Output is correct
39 Correct 547 ms 13272 KB Output is correct
40 Correct 553 ms 13328 KB Output is correct
41 Correct 544 ms 13284 KB Output is correct
42 Correct 278 ms 13808 KB Output is correct
43 Correct 110 ms 14804 KB Output is correct
44 Correct 122 ms 14784 KB Output is correct
45 Correct 123 ms 14792 KB Output is correct
46 Correct 128 ms 14828 KB Output is correct
47 Correct 130 ms 14784 KB Output is correct
48 Correct 118 ms 14796 KB Output is correct
49 Correct 122 ms 14784 KB Output is correct
50 Correct 145 ms 14836 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 304 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 304 KB Output is correct
7 Correct 1 ms 316 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 312 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 468 KB Output is correct
17 Correct 1 ms 448 KB Output is correct
18 Correct 1 ms 468 KB Output is correct
19 Correct 4 ms 1212 KB Output is correct
20 Correct 4 ms 1220 KB Output is correct
21 Correct 4 ms 1308 KB Output is correct
22 Correct 5 ms 1236 KB Output is correct
23 Correct 5 ms 1220 KB Output is correct
24 Correct 6 ms 1236 KB Output is correct
25 Correct 5 ms 1236 KB Output is correct
26 Correct 6 ms 1236 KB Output is correct
27 Correct 204 ms 36752 KB Output is correct
28 Correct 233 ms 38488 KB Output is correct
29 Correct 203 ms 36408 KB Output is correct
30 Correct 210 ms 36732 KB Output is correct
31 Correct 228 ms 36428 KB Output is correct
32 Correct 294 ms 40964 KB Output is correct
33 Correct 234 ms 41032 KB Output is correct
34 Correct 249 ms 41064 KB Output is correct
35 Correct 1 ms 300 KB Output is correct
36 Correct 198 ms 12472 KB Output is correct
37 Correct 1026 ms 36592 KB Output is correct
38 Correct 1036 ms 36600 KB Output is correct
39 Correct 1427 ms 35696 KB Output is correct
40 Correct 348 ms 23340 KB Output is correct
41 Correct 110 ms 12324 KB Output is correct
42 Correct 18 ms 2928 KB Output is correct
43 Correct 1041 ms 44396 KB Output is correct
44 Correct 2324 ms 44696 KB Output is correct
45 Correct 728 ms 36560 KB Output is correct
46 Correct 554 ms 36604 KB Output is correct
47 Correct 1280 ms 36224 KB Output is correct
48 Correct 571 ms 43668 KB Output is correct
49 Correct 534 ms 43960 KB Output is correct
50 Correct 527 ms 44160 KB Output is correct
51 Correct 7 ms 1928 KB Output is correct
52 Correct 7 ms 1912 KB Output is correct
53 Correct 7 ms 1844 KB Output is correct
54 Correct 11 ms 1868 KB Output is correct
55 Correct 11 ms 1804 KB Output is correct
56 Correct 11 ms 1828 KB Output is correct
57 Correct 73 ms 15292 KB Output is correct
58 Correct 70 ms 15340 KB Output is correct
59 Correct 91 ms 15116 KB Output is correct
60 Correct 145 ms 14708 KB Output is correct
61 Correct 138 ms 14796 KB Output is correct
62 Correct 179 ms 14824 KB Output is correct
63 Correct 547 ms 13272 KB Output is correct
64 Correct 553 ms 13328 KB Output is correct
65 Correct 544 ms 13284 KB Output is correct
66 Correct 278 ms 13808 KB Output is correct
67 Correct 110 ms 14804 KB Output is correct
68 Correct 122 ms 14784 KB Output is correct
69 Correct 123 ms 14792 KB Output is correct
70 Correct 128 ms 14828 KB Output is correct
71 Correct 130 ms 14784 KB Output is correct
72 Correct 118 ms 14796 KB Output is correct
73 Correct 122 ms 14784 KB Output is correct
74 Correct 145 ms 14836 KB Output is correct
75 Correct 270 ms 45440 KB Output is correct
76 Correct 258 ms 46844 KB Output is correct
77 Correct 268 ms 45452 KB Output is correct
78 Correct 260 ms 45172 KB Output is correct
79 Correct 315 ms 44980 KB Output is correct
80 Correct 244 ms 45112 KB Output is correct
81 Correct 900 ms 44364 KB Output is correct
82 Correct 837 ms 44168 KB Output is correct
83 Correct 810 ms 44120 KB Output is correct
84 Correct 1367 ms 44672 KB Output is correct
85 Correct 896 ms 44276 KB Output is correct
86 Correct 836 ms 44264 KB Output is correct
87 Correct 1020 ms 44336 KB Output is correct
88 Execution timed out 3025 ms 38312 KB Time limit exceeded
89 Halted 0 ms 0 KB -