您的位置:首页 > 其它

2018年全国多校算法寒假训练营练习比赛(第三场)H 向左走【极角排序】

2018-02-07 23:54 489 查看
题目链接:H 向左走

分析:

起点题目已给,每次左拐的角度最小就是最右,我用斜率判拐角wa了数次(肯定错呀),,,极角排序模板题.

不过你预处理出两个点,建立一个新的x轴,去暴力下一个点,再把之前的两个点更新,依次迭代应该也是可行的…

#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std;
typedef long long LL;

const int MAXN = 1e3 + 10;
int n, num;

struct Point {
int id;
double x, y;
}s[MAXN];

int cross(int x1, int y1, int x2, int y2) { //叉积判断方向
return (x1 * y2 - x2 * y1);
}

int dis(Point a, Point b) {
return ((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
}

int compare(Point a, Point b, Point c) {
return cross((b.x - a.x), (b.y - a.y), (c.x - a.x), (c.y - a.y));
}

bool cmp(Point a, Point b) {
if(compare(s[num], a, b) == 0) {
return dis(s[num], a) < dis(s[num], b);
}
return compare(s[num], a, b) > 0;
}

int main() {
while(scanf("%d", &n) != EOF) {
for(int i = 1; i <= n; i++) {
scanf("%d %lf %lf", &s[i].id, &s[i].x, &s[i].y);
if(i != 1 && s[i].y <= s[1].y) {
if(s[i].y == s[1].y && s[i].x < s[1].x)
swap(s[i], s[1]);
if(s[i].y < s[1].y) {
swap(s[i], s[1]);
}
}
}
num = 1;
for(int i = 2; i <= n; i++) {
sort(s + i, s + n + 1, cmp);
num++;
}
for(int i = 1; i < n; i++) {
printf("%d ", s[i].id);
}
printf("%d\n", s
.id);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐