Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
135 lines (114 loc) · 2.95 KB

File metadata and controls

135 lines (114 loc) · 2.95 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// Graham's convex hull
// input:
// list of points
// output:
// indicies of points that make up the convex hull
// Written by Dmitriy Okoneshnikov, 2021
#include <iostream>
#include <fstream>
#include <cmath>
#include <chrono>
#include <vector>
#include <limits>
#include <algorithm>
using namespace std;
typedef numeric_limits<double> dbl;
struct Point
{
double x;
double y;
Point(double x = 0.0, double y = 0.0)
: x(x), y(y)
{
}
Point operator-(Point pnt) { return Point(x - pnt.x, y - pnt.y); }
};
int getIndex(vector<Point> v, Point k)
{
for (int i = 0; i < v.size(); i++)
{
if (abs(v[i].x - k.x) < 10e-8 && abs(v[i].y - k.y) < 10e-8)
return i;
}
return -1;
}
ostream &operator<<(ostream &stream, const Point &p)
{
stream << p.x << " " << p.y;
return stream;
}
double vect_prod(Point A, Point B)
{
return A.x * B.y - A.y * B.x;
}
bool sort_func(Point a, Point b)
{
return vect_prod(a, b) > 0 || vect_prod(a, b) == 0 && (a.x * a.x + a.y * a.y < b.x * b.x + b.y * b.y);
}
vector<Point> convex_hull(vector<Point> points)
{
Point p0 = points[0];
for (Point p : points)
if (p.x < p0.x || (p.x == p0.x && p.y < p0.y))
p0 = p;
// сдвинуть в начало координат
for (Point &p : points)
{
p.x -= p0.x;
p.y -= p0.y;
}
// отсортировать по углу
sort(points.begin(), points.end(), &sort_func);
vector<Point> hull;
for (Point p : points)
{
// убираем последнюю точку минимальной выпуклой оболочки пока она выпуклая
while (hull.size() >= 2 && (vect_prod((p - hull.back()), (hull[hull.size() - 2] - hull.back())) <= 0))
hull.pop_back();
hull.push_back(p);
}
// сдвигаем обратно точки
for (Point &p : hull)
{
p.x += p0.x;
p.y += p0.y;
}
return hull;
}
int main(int argc, char **argv)
{
string input_filename = "input.txt";
string output_filename = "output.txt";
if (argc == 2)
{
input_filename = argv[1];
}
else if (argc >= 3)
{
input_filename = argv[1];
output_filename = argv[2];
}
ifstream inp(input_filename);
int n;
inp >> n;
vector<Point> arr;
for (int i = 0; i < n; i++)
{
Point temp;
inp >> temp.x;
inp >> temp.y;
arr.push_back(temp);
}
inp.close();
auto start = chrono::steady_clock::now();
vector<Point> ch = convex_hull(arr);
auto end = chrono::steady_clock::now();
auto diff = end - start;
ofstream out(output_filename, std::ios::out | std::ios::trunc);
out.precision(dbl::max_digits10);
out << chrono::duration<double, nano>(diff).count() << endl;
out << ch.size() << endl;
for (int i = 0; i < ch.size(); i++)
out << getIndex(arr, ch[i]) + 1 << " ";
out.close();
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.