HDU6325 (Interstellar Travel)[凸包,字典序]

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6325

注意观察每次移动的花费xi yj - xj yi,这是一个叉积。题目希望花费的负值最大,也就是所有的叉积和尽可能小。贪心考虑,我们需要让移动路线尽可能「上凸」,这样可以向顺时针转动的角度也会尽可能的大。于是维护一个上凸包即可。注意字典序的处理方式,拓展凸包时,如果在一个方向上遇到字典序较大的点,则不要弹出栈里的当前点。

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
136
137
138
139
140
141
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
const double eps=1e-8;
const double inf=1e20;
const double pi=acos(-1.0);
const int maxn=200009;
int sgn(double x) {
if(fabs(x)<eps) return 0;
if(x<0) return -1;
else return 1;
}
inline double sqr(double x) { return x*x; }
struct Point {
double x,y;
int index;
Point() {}
Point(double _x,double _y,int _index=0) {
x=_x;
y=_y;
index=_index;
}
double distance(Point p) {
return hypot(x-p.x,y-p.y);
}
double len() {
return hypot(x,y);
}
Point trunc(double r) {
double l=len();
if(!sgn(l)) return *this;
r/=l;
return Point(x*r,y*r);
}
bool operator ==(Point b) const {
return sgn(x-b.x)==0 && sgn(y-b.y)==0;
}
// bool operator <(Point b) const {
// return sgn(x-b.x)==0 ? sgn(y-b.y)<0:x<b.x;
// }
Point operator -(const Point& b) const {
return Point(x-b.x,y-b.y);
}
Point operator +(const Point& b) const {
return Point(x+b.x,y+b.y);
}
double operator ^(const Point& b) const {
return x*b.y-y*b.x;
}
double operator *(const Point& b) const {
return x*b.x+y*b.y;
}
Point rotleft() {
return Point(-y,x);
}
Point rotright() {
return Point(y,-x);
}
};
struct polygon {
int n;
Point p[maxn];
void add(Point q) {
p[n++]=q;
}
void init() {
n=0;
}
struct cmp {
Point p;
cmp(const Point& p0) { p=p0; }
bool operator ()(const Point& aa,const Point& bb) {
Point a=aa,b=bb;
int d=sgn((a-p)^(b-p));
if(d==0) {
return sgn(a.distance(p)-b.distance(p)) < 0;
}
return d < 0;
}
};
void norm() {
Point mi=p[0];
sort(p,p+n,cmp(mi));
}
void print() {
for(int i=0;i<n;i++) {
printf(i==n-1?"%d":"%d ",p[i].index);
}
printf("\n");
}
void Graham(polygon &convex) {
norm();
int &top=convex.n;
top=0;
if(n==1) {
top=1;
convex.p[0]=p[0];
return ;
}
if(n==2) {
top=2;
convex.p[0]=p[0];
convex.p[1]=p[1];
if(convex.p[0]==convex.p[1]) top--;
return ;
}
convex.p[0]=p[0];
convex.p[1]=p[1];
top=2;
for(int i=2;i<n;i++) {
while(top>1 && sgn((convex.p[top-1]-convex.p[top-2])
^(p[i]-convex.p[top-2]))>=0) {
if(sgn((convex.p[top-1]-convex.p[top-2])^(p[i]-convex.p[top-2]))==0
&& convex.p[top-1].index<p[i].index) break;
top--;
}
if(!(convex.p[top-1]==p[i]))
convex.p[top++]=p[i];
}
if(convex.n==2 && (convex.p[0]==convex.p[1])) convex.n--;
}
}poly,con;
int main() {
int T;
int n;
int x,y;
scanf("%d",&T);
while(T--) {
scanf("%d",&n);
poly.init();
for(int i=1;i<=n;i++) {
scanf("%d%d",&x,&y);
poly.add(Point(x,y,i));
}
con.init();
poly.Graham(con);
con.print();
}
}