洛谷P3138 [USACO16FEB]负载平衡Load Balancing_Silver

P3138 [USACO16FEB]负载平衡Load Balancing_Silver

题目描述

Farmer John's NN cows are each standing at distinct locations (x_1, y_1) ldots (x_n, y_n)(x1,y1)(xn,yn) on his two-dimensional farm (1 leq N leq 10001N1000, and the x_ixi's and y_iyi's are positive odd integers of size at most 1,000,0001,000,000). FJ wants to partition his field by building a long (effectively infinite-length) north-south fence with equation x=ax=a (aa will be an even integer, thus ensuring that he does not build the fence through the position of any cow). He also wants to build a long (effectively infinite-length) east-west fence with equation y=by=b, where bb is an even integer. These two fences cross at the point (a,b)(a,b), and together they partition his field into four regions.

FJ wants to choose aa and bb so that the cows appearing in the four resulting regions are reasonably "balanced", with no region containing too many cows. Letting MM be the maximum number of cows appearing in one of the four regions, FJ wants to make MM as small as possible. Please help him determine this

smallest possible value for MM.

给你一个矩阵,里面有些点,让你横向切一刀,纵向切一刀,使得得到的四个区域内的最大的点数最少。

输入输出格式

输入格式:

The first line of the input contains a single integer, NN. The next NN lines

each contain the location of a single cow, specifying its xx and yy

coordinates.

输出格式:

You should output the smallest possible value of MM that FJ can achieve by

positioning his fences optimally.

输入输出样例

输入样例#1: 复制
7
7 3
5 5
7 13
3 1
11 7
5 3
9 1
输出样例#1: 复制
2
/*
    预处理+枚举两条切线 
*/
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#define maxn 1005
using namespace std;
struct node{
    int x,y;
}a[maxn],b[maxn];
int c[maxn],top,L,R,l,r,now;
int n,m,ans=0x7fffffff;
bool cmp1(node u,node v){return u.x<v.x;}
bool cmp2(node u,node v){return u.y<v.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++;
    }
    now=1;
    for(int i=1;i<=top;i++){
        int kk=c[i]+1;
        for(;now<=n&&b[now].y<=kk;now++){
            if(b[now].x<k)L++;
            else R++;
        }
        ans=min(ans,max(max(L,l-L),max(R,r-R)));
    }
}
int main(){
    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,cmp1);
    sort(b+1,b+n+1,cmp2);
    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);
}
原文地址:https://www.cnblogs.com/thmyl/p/7716982.html