首页 > 其他 > 详细

UVA - 1606 Amphiphilic Carbon Molecules(两亲性分子)(扫描法)

时间:2017-02-06 14:27:13      阅读:153      评论:0      收藏:0      [点我收藏+]

题意:平面上有n(n <= 1000)个点,每个点为白点或者黑点。现在需放置一条隔板,使得隔板一侧的白点数加上另一侧的黑点数总数最大。隔板上的点可以看做是在任意一侧。

分析:枚举每个基准点i,将一条直线绕这个点旋转,每当扫过一个点,就可以动态修改两侧的点数。

#pragma comment(linker, "/STACK:102400000, 102400000")
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#define Min(a, b) ((a < b) ? a : b)
#define Max(a, b) ((a < b) ? b : a)
typedef long long LL;
typedef unsigned long long ULL;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};
const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};
const int MOD = 1e9 + 7;
const double pi = acos(-1.0);
const double eps = 1e-8;
const int MAXN = 1000 + 10;
const int MAXT = 10000 + 10;
using namespace std;
struct Point{
    int x, y;
    double r;
    bool operator < (const Point& rhs)const{
        return r < rhs.r;
    }
}num[MAXN], tmp[MAXN];
int color[MAXN];
bool Left(Point A, Point B){
    return A.x * B.y - A.y * B.x >= 0;
}
int main(){
    int n;
    while(scanf("%d", &n) == 1){
        if(!n) return 0;
        for(int i = 0; i < n; ++i){
            scanf("%d%d%d", &num[i].x, &num[i].y, &color[i]);
        }
        if(n <= 3){
            printf("%d\n", n);
            continue;
        }
        int ans = 0;
        for(int i = 0; i < n; ++i){//以i为基准点
            int id = 0;
            for(int j = 0; j < n; ++j){
                if(i != j){
                    tmp[id].x = num[j].x - num[i].x;
                    tmp[id].y = num[j].y - num[i].y;
                    if(color[j]){//遇到黑点的时候将其旋转180°,这样可以将统计一侧黑点和一侧白点转化为统计所有白点
                        tmp[id].x = -tmp[id].x;
                        tmp[id].y = -tmp[id].y;
                    }
                    tmp[id].r = atan2(tmp[id].y, tmp[id].x);
                    ++id;
                }
            }
            sort(tmp, tmp + id);
            int l = 0, r = 0, cnt = 2;
            while(l < id){
                if(r == l){
                    r = (r + 1) % id;
                    ++cnt;
                }
                while(r != l && Left(tmp[l], tmp[r])){
                    r = (r + 1) % id;
                    ++cnt;
                }
                --cnt;
                ++l;
                ans = Max(ans, cnt);
            }
        }
        printf("%d\n", ans);
    }
    return 0;
}

  

UVA - 1606 Amphiphilic Carbon Molecules(两亲性分子)(扫描法)

原文:http://www.cnblogs.com/tyty-Somnuspoppy/p/6369948.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!