AtCoder Beginner Contest 373

AtCoder Beginner Contest 373

G - No Cross Matching

一定存在解。当距离和最小时一定是一组可行解,跑最小费用最大流。

或者一直重复寻找两个相交的线段,将端点互换。

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
#include <bits/stdc++.h>

using i64 = long long;

using T = int;

struct Point {
T x;
T y;
Point(T x = 0, T y = 0) : x(x), y(y)
{
}

Point &operator+=(const Point &p)
{
x += p.x, y += p.y;
return *this;
}
Point &operator-=(const Point &p)
{
x -= p.x, y -= p.y;
return *this;
}
Point &operator*=(const T &v)
{
x *= v, y *= v;
return *this;
}
friend Point operator-(const Point &p)
{
return Point(-p.x, -p.y);
}
friend Point operator+(Point lhs, const Point &rhs)
{
return lhs += rhs;
}
friend Point operator-(Point lhs, const Point &rhs)
{
return lhs -= rhs;
}
friend Point operator*(Point lhs, const T &rhs)
{
return lhs *= rhs;
}
};

T cross(const Point &a, const Point &b)
{
return a.x * b.y - a.y * b.x;
}

const double eps = 1e-8;

int sign(double x) // 符号函数
{
if (fabs(x) < eps)
return 0;
if (x < 0)
return -1;
return 1;
}

bool segment_intersection(Point a1, Point a2, Point b1, Point b2) // 没有判断在一条直线上,要先做快速排斥
{
double c1 = cross(a2 - a1, b1 - a1), c2 = cross(a2 - a1, b2 - a1);
double c3 = cross(b2 - b1, a2 - b1), c4 = cross(b2 - b1, a1 - b1);
return sign(c1) * sign(c2) <= 0 && sign(c3) * sign(c4) <= 0;
}

void solve()
{
int n;
std::cin >> n;
std::vector<Point> a(n + 1), b(n + 1);
for (int i = 1; i <= n; i++) {
std::cin >> a[i].x >> a[i].y;
}
for (int i = 1; i <= n; i++) {
std::cin >> b[i].x >> b[i].y;
}

std::vector<int> ans(n + 1);
std::iota(ans.begin(), ans.end(), 0);

auto check = [&]() -> bool {
for (int i = 1; i <= n; i++) {
for (int j = i + 1; j <= n; j++) {
if (segment_intersection(a[i], b[ans[i]], a[j], b[ans[j]])) {
std::swap(ans[i], ans[j]);
return true;
}
}
}
return false;
};

while (check())
;
for (int i = 1; i <= n; i++) {
std::cout << ans[i] << " \n"[i == n];
}
}

int main()
{
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);

int t = 1;
// std::cin >> t;
while (t--)
solve();

return 0;
}

AtCoder Beginner Contest 373
http://example.com/2024/10/05/AtCoder-Beginner-Contes-373/
作者
muxingchengfeng
发布于
2024年10月5日
许可协议