[COCI2009]Dvapravca

随机化真好玩

#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include<algorithm>
#include<vector>
#include<ctime>
#include<cstdlib>
#include<iostream>
#include<cstdio>
#include<cmath>
typedef long double ld;
const int maxn = 1010;
int x[maxn],y[maxn];
int cl[maxn];
int per[maxn];
ld val[maxn];
ld R;
struct T{
    inline __attribute__((always_inline)) bool operator () (const int&a,const int&b)const{
        return val[a] < val[b];
    }
};
inline void up(int&x,int y){if(x<y)x=y;}
inline void down(int&x,int y){if(x>y)x=y;}
inline void up(ld&x,ld y){if(x<y)x=y;}
inline void down(ld&x,ld y){if(x>y)x=y;}
int n;
inline int solve(){
    int a=rand()%n+1,b;
    do b=rand()%n+1;while(b==a);
    if(cl[a]&&cl[b]){
        if(x[a]==x[b]){
            int min=-2e9,max=2e9;
            for(int i=1;i<=n;++i)if(!cl[i])if(x[i]<x[a])up(min,x[i]);else down(max,x[i]);
            int ret=0;
            for(int i=1;i<=n;++i)if(cl[i])ret+=min<=x[i]&&x[i]<=max;
            return ret;
        }else{
            ld k = (ld)(y[a]-y[b])/(x[a]-x[b]);
            ld p = y[b] - x[b] * k;
            ld min=-2e30,max=2e30;
            for(int i=1;i<=n;++i)if(!cl[i]){
                ld o = y[i] - x[i] * k;
                if(o<p)up(min,o);else down(max,o);
            }
            int ret=0;
            for(int i=1;i<=n;++i)if(cl[i]){
                ld o = y[i] - x[i] * k;
                ret += min <= o && o <= max;
            }
            return ret;
        }
    }
    return 0;
}
int main(){
    srand(time(0) ^ * new unsigned long long);
    size_t t = clock();
    std::ios::sync_with_stdio(false),std::cin.tie(0);
    char ch;
    std::cin >> n;
    int cnt=0;
    for(int i=1;i<=n;++i){
        std::cin >> x[i] >> y[i] >> ch;
        cl[i] = ch == 'R',per[i]=i;
        cnt += cl[i];
    }
    if(cnt == n)
        return std::cout << n << '\n',0;
    if(cnt == 0)
        return std::cout << 0 << '\n',0;
    int ans=0;
    while((double)(clock()-t)/CLOCKS_PER_SEC < 0.5)
        up(ans,solve());
    std::vector<ld> v;
    for(int i=1;i<=n;++i){
        for(int j=i+1;j<=n;++j)
            if(cl[i] + cl[j] == 1){
                ld x1=x[i],y1=y[i];
                ld x2=x[j],y2=y[j];
                v.push_back(-(y1-y2)/(x1-x2)+1e-10);
                v.push_back(-(y1-y2)/(x1-x2)-1e-10);
            }
    }
    std::sort(v.begin(),v.end());
    v.erase(std::unique(v.begin(),v.end()),v.end());
    std::random_shuffle(v.begin(),v.end());
    for(std::vector<ld>::iterator i=v.begin();i!=v.end();++i)if(i==i){
        if((double)(clock()-t)/CLOCKS_PER_SEC > 0.996)break;
        R = *i;
        for(int i=1;i<=n;++i)val[i]=R*x[i]+y[i];
        std::sort(per+1,per+n+1,T());
        int sum=0;
        for(int i=1;i<=n;++i){
            if(cl[per[i]])++sum;
            else up(ans,sum),sum=0;
        }
        up(ans,sum);
    }
    std::cout << ans << '\n';
}

猜你喜欢

转载自www.cnblogs.com/skip1978/p/10335517.html