CSES - Aalto Competitive Programming 2024 - wk10 - Wed - Results
Submission details
Task:Closest points
Sender:aalto2024k_007
Submission time:2024-11-13 16:40:33 +0200
Language:C++ (C++17)
Status:READY
Result:
Test results
testverdicttime
#10.00 sdetails
#20.11 sdetails
#30.19 sdetails
#40.16 sdetails
#5ACCEPTED0.00 sdetails
#60.11 sdetails
#7ACCEPTED0.00 sdetails
#8ACCEPTED0.00 sdetails
#9ACCEPTED0.00 sdetails
#100.12 sdetails
#11ACCEPTED0.00 sdetails
#12ACCEPTED0.00 sdetails
#130.11 sdetails
#14ACCEPTED0.00 sdetails
#150.00 sdetails
#160.12 sdetails
#170.11 sdetails
#18ACCEPTED0.00 sdetails

Compiler report

input/code.cpp: In function 'long long int stripClosest(std::vector<Point>&, long long int)':
input/code.cpp:42:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<Point>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   42 |     for (int i = 0; i < strip.size(); ++i) {
      |                     ~~^~~~~~~~~~~~~~
input/code.cpp:43:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<Point>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   43 |         for (int j = i + 1; j < strip.size() && (strip[j].y - strip[i].y) < minDist; ++j) {
      |                             ~~^~~~~~~~~~~~~~

Code

#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
#include <limits.h>

using namespace std;

struct Point{
    long long x, y;
};

long long compareX(Point& a, Point& b){
    return a.x - b.x;
}

long long compareY(Point& a, Point& b){
    return a.y - b.y;
}

long long dist(Point a, Point b) 
{ 
    return (a.x - b.x)*(a.x - b.x) + (a.y - b.y)*(a.y - b.y); 
} 

long long bruteForce(vector<Point>& points, long long left, long long right) {
    long long minDist = numeric_limits<long long>::max();
    for (int i = left; i < right; ++i) {
        for (int j = i + 1; j < right; ++j) {
            long long d = dist(points[i], points[j]);
            if (d < minDist) {
                minDist = d;
            }
        }
    }
    return minDist;
}

long long stripClosest(vector<Point>& strip, long long d) {
    long long minDist = d;
    sort(strip.begin(), strip.end(), compareY);
    for (int i = 0; i < strip.size(); ++i) {
        for (int j = i + 1; j < strip.size() && (strip[j].y - strip[i].y) < minDist; ++j) {
            long long d = dist(strip[i], strip[j]);
            if (d < minDist) {
                minDist = d;
            }
        }
    }
    return minDist;
}

long long closest(vector<Point>& points, long long left, long long right) {
    if (right - left <= 3) {
        return bruteForce(points, left, right);
    }

    long long mid = left + (right - left) / 2;
    Point midPoint = points[mid];

    long long dl = closest(points, left, mid);
    long long dr = closest(points, mid, right);

    long long d = min(dl, dr);

    vector<Point> strip;
    for (int i = left; i < right; ++i) {
        if (abs(points[i].x - midPoint.x) < d) {
            strip.push_back(points[i]);
        }
    }

    return min(d, stripClosest(strip, d));
}


int main() {
    int n;
    cin >> n;
    vector<Point> points(n);
    
    for (int i = 0; i < n; ++i){
        cin >> points[i].x >> points[i].y;
    }

    sort(points.begin(), points.end(), compareX);

    cout << closest(points, 0, n) << endl;
}

Test details

Test 1

Verdict:

input
100
58 36
81 -7
46 49
87 -58
...

correct output
1

user output
(empty)

Test 2

Verdict:

input
200000
-222 -705
277 680
-436 561
528 -516
...

correct output
1

user output
(empty)

Test 3

Verdict:

input
200000
-464738043 865360844
465231470 129093134
-276549869 -21946314
111055008 -48821736
...

correct output
25413170

user output
(empty)

Test 4

Verdict:

input
200000
1 513001000
2 689002000
3 785003000
4 799004000
...

correct output
1000000

user output
(empty)

Test 5

Verdict: ACCEPTED

input
4
0 0
0 3
3 0
1 1

correct output
2

user output
2

Test 6

Verdict:

input
200000
1 0
1 1
1 2
1 3
...

correct output
1

user output
(empty)

Test 7

Verdict: ACCEPTED

input
4
1 2
10 3
3 5
8 5

correct output
8

user output
8

Test 8

Verdict: ACCEPTED

input
4
10 6
4 10
8 3
2 3

correct output
13

user output
13

Test 9

Verdict: ACCEPTED

input
2
-999999999 -999999999
999999999 999999999

correct output
7999999984000000008

user output
7999999984000000008

Test 10

Verdict:

input
200000
0 1
1 1
2 1
3 1
...

correct output
1

user output
(empty)

Test 11

Verdict: ACCEPTED

input
8
1 10000
-1 -10000
2 0
-2 0
...

correct output
16

user output
16

Test 12

Verdict: ACCEPTED

input
3
-1000000000 -1000000000
1000000000 1000000000
0 0

correct output
2000000000000000000

user output
2000000000000000000

Test 13

Verdict:

input
199999
1 1
2 1
3 1
4 1
...

correct output
1

user output
(empty)

Test 14

Verdict: ACCEPTED

input
4
0 0
5 8
6 1
10000 0

correct output
37

user output
37

Test 15

Verdict:

input
435
-842 -199
-480 798
-176 -406
792 608
...

correct output
2

user output
(empty)

Test 16

Verdict:

input
200000
1 0
1 2
1 4
1 6
...

correct output
4

user output
(empty)

Test 17

Verdict:

input
200000
0 1
2 1
4 1
6 1
...

correct output
4

user output
(empty)

Test 18

Verdict: ACCEPTED

input
3
-1000000000 -1000000000
1000000000 1000000000
1000000000 -1000000000

correct output
4000000000000000000

user output
4000000000000000000