Skip to content

Commit 76d65b1

Browse files
authored
Class #09
1 parent 1895478 commit 76d65b1

File tree

2 files changed

+135
-0
lines changed

2 files changed

+135
-0
lines changed

Kattis Closest Pair.cpp

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
//
2+
// Created by Alon on 02/06/2020.
3+
//
4+
5+
#include <iostream>
6+
#include <cstdlib>
7+
#include <cstring>
8+
#include <fstream>
9+
#include <string>
10+
#include <vector>
11+
#include <algorithm>
12+
#include <set>
13+
#include <stack>
14+
#include <cassert>
15+
#include <cfloat>
16+
#include <cmath>
17+
#include <iomanip>
18+
19+
using namespace std;
20+
typedef long long ll;
21+
22+
23+
double dist(pair<double, double> p1, pair<double, double> p2) {
24+
return hypot(p1.first - p2.first, p1.second - p2.second);
25+
}
26+
27+
/// Closest Pair (Uniform) Kattis - closestpair1
28+
int main() {
29+
int n;
30+
while (cin >> n && n) {
31+
vector<pair<double, double>> axis;
32+
while (n--) {
33+
pair<double, double> p;
34+
cin >> p.first >> p.second;
35+
axis.push_back(p);
36+
}
37+
pair<double, double> p1, p2;
38+
double d = 100001, i_to_remove = 0;
39+
sort(axis.begin(), axis.end());
40+
set<pair<double, double>> ys;
41+
for (int i = 0; i < axis.size(); ++i) {
42+
pair<double, double> main_p = axis[i];
43+
auto it = ys.lower_bound({main_p.second - d, -100001});
44+
while (it != ys.end() && (*it).first <= main_p.second + d) {
45+
pair<double, double> temp_p = {(*it).second, (*it).first}; // x,y
46+
double temp_d = dist(main_p, temp_p);
47+
if (temp_d < d) {
48+
d = temp_d;
49+
p1 = temp_p;
50+
p2 = main_p;
51+
}
52+
it++;
53+
}
54+
while (i_to_remove < i && axis[i_to_remove].first < axis[i].first - d) {
55+
pair<double, double> temp_p = {axis[i_to_remove].second, axis[i_to_remove].first};
56+
ys.erase(temp_p);
57+
++i_to_remove;
58+
}
59+
ys.insert(make_pair(main_p.second, main_p.first));
60+
}
61+
cout << fixed << setprecision(2)<< p1.first << " " << p1.second << " " << p2.first << " " << p2.second << endl;
62+
}
63+
64+
return 0;
65+
}

Kattis Platforme.cpp

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
//
2+
// Created by Alon on 02/06/2020.
3+
//
4+
5+
#include <iostream>
6+
#include <cstdlib>
7+
#include <cstring>
8+
#include <fstream>
9+
#include <string>
10+
#include <vector>
11+
#include <algorithm>
12+
#include <set>
13+
#include <stack>
14+
#include <cassert>
15+
16+
using namespace std;
17+
typedef long long ll;
18+
19+
enum edge {END = 0, START = 1};
20+
struct pillar {
21+
int y, x1, x2;
22+
};
23+
24+
int main() {
25+
int N, res = 0;
26+
cin >> N;
27+
vector<pillar> pillars(N); // Y X1 X2
28+
// vector<pair<int, int>> starts(N); // X1 N
29+
// vector<pair<int, int>> ends(N); // X2 N
30+
vector<pair<int, int>> axis; // X2 N
31+
for (int i = 0; i < N; ++i) {
32+
cin >> pillars[i].y >> pillars[i].x1 >> pillars[i].x2;
33+
// starts[i] = {pillars[i].x1, i};
34+
// ends[i] = {pillars[i].x2, i};
35+
axis.push_back(make_pair(pillars[i].x1, i)); // start
36+
axis.push_back(make_pair(pillars[i].x2, i)); // end
37+
}
38+
// sort(starts.begin(), starts.end());
39+
// sort(ends.begin(), ends.end());
40+
sort(axis.begin(), axis.end());
41+
set<int> current_heights = { 0 };
42+
int i = -1;
43+
while (i + 1 < (int)axis.size()) {
44+
stack<int> pillar_to_remove;
45+
vector<int> pillar_to_add;
46+
do {
47+
++i;
48+
int number_of_pillar = axis[i].second;
49+
if (axis[i].first == pillars[number_of_pillar].x1) { /// begin pillar
50+
pillar_to_add.push_back(number_of_pillar);
51+
} else { /// end pillar
52+
assert(axis[i].first == pillars[number_of_pillar].x2);
53+
pillar_to_remove.push(number_of_pillar);
54+
int len = pillars[number_of_pillar].y - *(--(current_heights.find(pillars[number_of_pillar].y))); // upper_bound is exclusive
55+
res += len;
56+
}
57+
} while (i + 1 < axis.size() && axis[i].first == axis[i+1].first);
58+
while (!pillar_to_remove.empty()) {
59+
current_heights.erase(pillars[pillar_to_remove.top()].y);
60+
pillar_to_remove.pop();
61+
}
62+
for (int p : pillar_to_add) current_heights.insert(pillars[p].y);
63+
for (int p : pillar_to_add) {
64+
int len = pillars[p].y - *(--(current_heights.find(pillars[p].y))); // upper_bound is exclusive
65+
res += len;
66+
}
67+
}
68+
cout << res << endl;
69+
return 0;
70+
}

0 commit comments

Comments
 (0)