# BZOJ 1696 [Usaco2007 Feb]Building A New Barn新牛舍 数学

## 代码:

#include <map>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define N 10010
#define INF 0x3f3f3f3f
#define pa pair<int,int>
using namespace std;
int n;
int a[N];
int b[N];
int xx[5]={0,-1,1,0,0};
int yy[5]={0,0,0,1,-1};
struct node
{
int x,y;
}c[N];
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%d%d",&a[i],&b[i]);
c[i].x=a[i],c[i].y=b[i];
}
sort(a+1,a+n+1),sort(b+1,b+n+1);
if(n&1)
{
int tmp=n/2+1;
int tmpx=a[tmp],tmpy=b[tmp];
int ans=0;
int flag=1;
for(int i=1;i<=n;i++)
{
if(tmpx==c[i].x&&tmpy==c[i].y){flag=0;break;}
}
if(flag)
{
for(int i=1;i<=n;i++)
{
ans+=abs(tmpx-a[i])+abs(tmpy-b[i]);
}
printf("%d 1\n",ans);
}else
{
ans=INF;
int cnt=0;
for(int i=1;i<=4;i++)
{
int x=tmpx+xx[i],y=tmpy+yy[i];
int ret=0;
for(int j=1;j<=n;j++)
{
ret+=abs(x-c[j].x)+abs(y-c[j].y);
}
if(ret<ans)ans=ret,cnt=1;
else if(ret==ans)cnt++;
}
printf("%d %d\n",ans,cnt);
}
}else
{
int tmpx1=a[n/2],tmpx2=a[n/2+1];
int tmpy1=b[n/2],tmpy2=b[n/2+1];
int ret=0,cnt=(tmpx2-tmpx1+1)*(tmpy2-tmpy1+1);
for(int k=1;k<=n;k++)
{
if(c[k].x>=tmpx1&&c[k].x<=tmpx2&&c[k].y>=tmpy1&&c[k].y<=tmpy2)cnt--;
ret+=abs(tmpx1-c[k].x)+abs(tmpy1-c[k].y);
}
printf("%d %d\n",ret,cnt);
}
} 
• 0
点赞
• 0
评论
• 0
收藏
• 一键三连
• 扫一扫，分享海报

11-01 556

12-18 94
12-24 51
09-07 70
03-10 352
07-12 355

wzq_QwQ

¥2 ¥4 ¥6 ¥10 ¥20