原题: http://acm.hdu.edu.cn/showproblem.php?pid=6325
题意:二维平面上给出许多点,起点(0,0)终点(m,0)。选一些点,相邻两个点i,j之间产生一个权值,(xi× yj − xj×yi),其实就是叉积。 要求权值和最小,起点和终点必须选。点可以重合,如果有多个答案要求输出字典序最小的。
这个叉积就可以转换成一个图形的面积,它又要求这个值最小,其实就是要求面积最大(因为是顺时针的吧?)。这个方块必须是凸着的。 然后就变成求一个凸包了。
#include <bits/stdc++.h>
using namespace std;
int t,n;
struct point {
// x,y要开long long
long long x,y;
int id;
friend point operator -(const point &a,const point &b) {
point c;
c.x = a.x - b.x;
c.y = a.y - b.y;
return c;
}
friend long long operator ^(point &a,point &b) {
return a.x * b.y - a.y * b.x;
}
};
point a[200005];
// 按x从小到大,y从小到大排序
bool cmp(point a,point b) {
if(a.x == b.x) {
if(a.y == b.y) {
return a.id < b.id;
}
else {
return a.y > b.y;
}
}
return a.x < b.x;
}
point s[200005];
int top = 0;
bool judge(point a,point b,point c) {
// 这里两个点应该可以看成是向量吧?
point fi = b - a;
point se = c - a;
// 返回true的话,代表栈中的那个点不是凸包的点
if((fi ^ se) > 0) return true;
else if((fi ^ se) == 0) {
// 共线的话要看谁的id小
return c.id < b.id;
}
else return false;
}
int main() {
scanf("%d",&t);
while(t--) {
scanf("%d",&n);
for(int i = 1; i <= n; i++) {
scanf("%lld%lld",&a[i].x,&a[i].y);
a[i].id = i;
}
sort(a + 1,a + 1 + n,cmp);
// Graham扫描法?是这个思想吧
top = 0;
s[top++] = a[1];
s[top++] = a[2];
for(int i = 3; i <= n; i++) {
// 两个点重合了,后面的那个点就不用选了
if(s[top - 1].x == a[i].x && s[top - 1].y == a[i].y) continue;
while(top >= 2 && judge(s[top - 2],s[top - 1],a[i])) {
top--;
}
s[top++] = a[i];
}
for(int i = 0; i < top; i++) {
printf("%d%c",s[i].id,i == top - 1 ? '\n' : ' ');
}
}
return 0;
}