您的位置:首页 > 其它

UVA - 1606 Amphiphilic Carbon Molecules 极角扫描法

2018-07-12 20:43 627 查看

题目:点击查看题目

思路:这道题的解决思路是极角扫描法。极角扫描法的思想主要是先选择一个点作为基准点,然后求出各点对于该点的相对坐标,同时求出该坐标系下的极角,按照极角对点进行排序。然后选取点与基准点形成的线对点进行扫描,基准线为遍历选取,扫描线扫过的点,减去基准线扫过的点即为所要求的点的数量。同时注意到我们要求的是线两边的两种点的数量,于是一种点比如黑点可以旋转180度,然后之考察这180度内的百点数量即可。本题的基准点选取复杂度为O(n),极角排序复杂度O(nlogn),扫描复杂度O(n),复杂度为O(N2logN + N2),可视为O(N2logN)

AC代码:

#include <iostream>
#include <cmath>
#include <algorithm>

using namespace std;

const int maxn = 1000 + 5;

struct point{
int x, y, color;
double angle;
bool operator < (const point& temp) const {
return angle < temp.angle;
}
}point[maxn], cpoint[maxn];

bool judge(struct point& A, struct point& B) {
return A.x*B.y - A.y*B.x >= 0;
}

int solve(int n) {
if(n <= 3) return n;

int ans = 0;
for(int i = 0; i < n; i++) {
int index = 0;
for(int j = 0; j < n; j++) {
if(j == i) continue;
cpoint[index].x = point[j].x - point[i].x;
cpoint[index].y = point[j].y - point[i].y;
if(point[j].color) {
cpoint[index].x = -cpoint[index].x;
cpoint[index].y = -cpoint[index].y;
}
cpoint[index].angle = atan2(cpoint[index].y, cpoint[index].x);
index++;
}
sort(cpoint, cpoint+index);

int st = 0, ed = 0, cnt = 2;
while(st < index) {
if(st == ed) {
ed = (ed + 1)%index;
cnt++;
}
while(st != ed && judge(cpoint[st], cpoint[ed])) {
ed = (ed + 1)%index;
cnt++;
}

cnt--;
st++;

ans = max(ans, cnt);
}
}
return ans;
}

int main()
{
int n;
while(cin >> n && n) {
for(int i = 0; i < n; i++) {
cin >> point[i].x >> point[i].y >> point[i].color;
}
cout << solve(n) << endl;
}
return 0;
}

 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: