UVa OJ 1606 - Amphiphilic Carbon Molecules

UVa OJ 1606 - Amphiphilic Carbon Molecules

Problem

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

Input

不超过10个测试情况。每种情况以一个整数N开始,代表点的个数,接着输入N行x,y,r,分别表示坐标点的x坐标,y坐标和点的颜色,其中1是黑色。输入以0结束。

Output

For each test case, output a line containing a single integer, which is the maximum number of dots we count.

Sample Input

3
0 0 0
0 1 0
2 2 1
4
0 0 0
0 4 0
4 0 0
1 2 1
7
-1 0 0
1 2 1
2 3 0
2 1 1
0 3 1
1 4 0
-1 2 0
0

Sample Output

3
3
6

Solution

扫描问题。只需要以一个点为基准点,然后做线,统计两边的点的数量就行。


在做这道题的时候,只是一个小小等式的区别,导致我TLE了好多次。后来也是测试了好久,才发现了这个问题。究其原因,还是取模太耗费时间,于是我就把取模的部分做了点修改,最终成功通过。


这里我利用先排序的方式,选好基准点之后,对每个点关于基准点的角度进行排序,然后才开始真正的扫描。

#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;

const int N=1001;

class Point{
public:
    int x,y,color;
    double rad;
    bool operator<(const Point &rhs)const{
        return rad<rhs.rad;
    }
}dot[N],tempDot[N];

int n, num;

bool isInArea(Point A,Point B){
    return A.x*B.y-A.y*B.x>=0;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);

    while(cin>>n && n){
        for(int i=0;i<(n);i++)
            cin >> dot[i].x >> dot[i].y >> dot[i].color;
        if(n<=2) return 2;
        int ans=0;

        for(int i=0;i<n;i++){
            num=0;
            for(int j=0;j<n;j++){
                if(j == i) continue;
                tempDot[num].x=dot[j].x-dot[i].x;
                tempDot[num].y=dot[j].y-dot[i].y;
                if(dot[j].color){
                    tempDot[num].x=-tempDot[num].x;
                    tempDot[num].y=-tempDot[num].y;
                }
                tempDot[num].rad=atan2(tempDot[num].y,tempDot[num].x);
                num++;
            }
            sort(tempDot,tempDot+num);

            int divi=0,scani=0,cnt=2;
            while(divi<num){
            if(scani==divi){
                scani=scani+1;
                if(scani >= num) scani = scani-num;
                cnt++;
            }
            while(scani!=divi && isInArea(tempDot[divi],tempDot[scani])){
                scani=scani+1;
                if(scani >= num) scani = scani-num;
                cnt++;
            }
            cnt--;
            divi++;
            ans=max(ans,cnt);
            }
        }
        cout << ans << '\n';
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值