模拟——洛谷P3138 [USACO16FEB]负载平衡Load Balancing

https://www.luogu.org/problem/show?pid=3138
这一题你做一点预处理,然后直接枚举两条切线就好了;
很简单的一个离散;
好像也不算说明离散;

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cstring>
using namespace std;
struct cs{
    int x,y;
}a[1005],b[1005];
int c[1005],top;
int L,R,l,r,o;
int n,m,ans;
bool cmp(cs a,cs b){return a.x<b.x;}
bool cmp2(cs a,cs b){return a.y<b.y;}
void cfb(int k){
    l=r=L=R=0;
    for(int i=1;i<=n;i++)
        if(a[i].x<k)++l;else ++r;//统计切线分成的两半的点数 
    o=1;
    for(int i=1;i<=top;i++){
        int kk=c[i]+1;//枚举切线 
        for(;o<=n&&b[o].y<=kk;++o)
            if(b[o].x<k)++L;else ++R;//统计点数 
        ans=min(ans,max(max(L,l-L),max(R,r-R)));//更新答案; 
    }   
}
int main()
{
    ans=1e9;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)scanf("%d%d",&a[i].x,&a[i].y),b[i]=a[i];
    sort(a+1,a+n+1,cmp);//按x递增排序 
    sort(b+1,b+n+1,cmp2);//按y 
    for(int i=1;i<=n;i++)if(b[i].y!=c[top])c[++top]=b[i].y;//这个判重一定要判 
    for(int i=1;i<=n;i++)cfb(a[i].x+1);//枚举切线 
    printf("%d",ans);
}
posted @ 2017-04-07 14:09  largecube233  阅读(126)  评论(0编辑  收藏  举报