NC24045. [USACO 2016 Feb S]Load Balancing
描述
输入描述
The first line of the input contains a single integer, N. The next N lines each contain the location of a single cow, specifying its x and y coordinates.
输出描述
You should output the smallest possible value of M 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
输出:
2
C++(clang++11) 解法, 执行用时: 8ms, 内存消耗: 384K, 提交时间: 2020-12-13 20:01:55
#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); }