别管代码长不长,我把他排完序输出然后发现并不符合我的排序要求,有没有大佬解释一下,谢谢。
#include <cmath>
#include <queue>
#include <iostream>
#include <algorithm>
using namespace std;
const double inf = 1e9;
int n, cnt;
bool f[100005];
double cur_k = 1000000000, ans, aa, bb, r;
int st[100005], tp = 1;
struct Node {double x, y;}a[100005];
bool cmp (Node n1, Node n2) {
if (n1.x < n2.x) return true;
if (n1.x == n2.x) {
if (n1.y < n2.y) return true;
return false;
}
return false;
}
double dist (int n1, int n2) {
return sqrt ( (a[n1].x - a[n2].x) * (a[n1].x - a[n2].x) + (a[n1].y - a[n2].y) * (a[n1].y - a[n2].y) );
}
double k (int n1, int n2) {
if (a[n1].x == a[n2].x) return inf;
return (a[n1].y - a[n2].y) / (a[n1].x - a[n2].x);
}
int main () {
cin >> n >> aa >> bb >> r;
aa /= 2; bb /= 2;
aa -= r; bb -= r;
for (int i = 1; i <= n; i ++) {
double cx, cy, t, x, y;
cin >> cx >> cy >> t;
y = sin (t) * bb + cos (t) * aa;
x = cos (t) * bb - sin (t) * aa;
a[++ cnt].x = x + cx;
a[cnt].y = y + cy;
y = sin (t) * -bb + cos (t) * aa;
x = cos (t) * -bb - sin (t) * aa;
a[++ cnt].x = x + cx;
a[cnt].y = y + cy;
y = sin (t) * bb + cos (t) * -aa;
x = cos (t) * bb - sin (t) * -aa;
a[++ cnt].x = x + cx;
a[cnt].y = y + cy;
y = sin (t) * -bb + cos (t) * -aa;
x = cos (t) * -bb - sin (t) * -aa;
a[++ cnt].x = x + cx;
a[cnt].y = y + cy;
}
sort (a + 1, a + cnt + 1, cmp);
st[1] = 1;
for (int i = 1; i <= cnt; i ++) cout << a[i].x << " " << a[i].y << "\n";
for (int i = 2; i <= cnt; i ++) {
while (tp != 1 && k (i, st[tp]) >= k (st[tp], st[tp - 1] ) ) {
ans -= dist (st[tp], st[tp - 1]);
f[st[tp --] ] = false;
}
ans += dist (i, st[tp]);
st[++ tp] = i;
f[i] = true;
}
while (tp != 1) -- tp;
st[1] = n;
for (int i = cnt - 1; i >= 1; i --) {
if (f[i]) continue;
while (tp != 1 && k (i, st[tp]) >= k (st[tp], st[tp - 1] ) ) {
ans -= dist (st[tp], st[tp - 1]);
f[st[tp --] ] = false;
}
ans += dist (i, st[tp]);
st[++ tp] = i;
f[i] = true;
}
printf ("%.2lf", ans + 2 * 3.1415926 * r);
return 0;
}