bzoj 1696: [Usaco2007 Feb]Building A New Barn新牛舍

1696: [Usaco2007 Feb]Building A New Barn新牛舍

Time Limit: 5 Sec   Memory Limit: 64 MB
Submit: 481   Solved: 217
[ Submit][ Status][ Discuss]

4
1 -3
0 1
-2 1
1 -1

Sample Output

10 4

#include<stdio.h>
#include<map>
#include<stdlib.h>
#include<algorithm>
using namespace std;
map<pair<int, int>, int> p;
int a[10005], b[10005];
int dir[4][2] = {1,0,0,1,-1,0,0,-1};
int main(void)
{
int n, i, j, x1, y1, x2, y2, sum, bet, ans;
scanf("%d", &n);
for(i=1;i<=n;i++)
{
scanf("%d%d", &a[i], &b[i]);
p[make_pair(a[i], b[i])];
}
sort(a+1, a+n+1);
sort(b+1, b+n+1);
ans = sum = 0, bet = 2147483647;
if(n%2)
{
x1 = a[(n+1)/2], y1 = b[(n+1)/2];
if(p.count(make_pair(x1, y1)))
{
for(i=0;i<=3;i++)
{
sum = 0;
if(p.count(make_pair(x1+dir[i][0], y1+dir[i][1])))
continue;
for(j=1;j<=n;j++)
{
sum += abs(a[j]-(x1+dir[i][0]));
sum += abs(b[j]-(y1+dir[i][1]));
}
if(sum<bet)
{
bet = sum;
ans = 1;
}
else if(sum==bet)
ans++;
}
}
else
{
for(j=1;j<=n;j++)
{
sum += abs(a[j]-x1);
sum += abs(b[j]-y1);
}
bet = sum, ans = 1;
}
}
else
{
x1 = a[n/2], y1 = b[n/2];
x2 = a[n/2+1], y2 = b[n/2+1];
for(j=1;j<=n;j++)
{
sum += abs(a[j]-x1);
sum += abs(b[j]-y1);
}
bet = sum, ans = (x2-x1+1)*(y2-y1+1);
if(p.count(make_pair(x1, y1)))  ans--;
if(p.count(make_pair(x1, y2)))  ans--;
if(p.count(make_pair(x2, y2)))  ans--;
if(p.count(make_pair(x2, y1)))  ans--;
}
printf("%d %d\n", bet, ans);
return 0;
}

• 1
点赞
• 0
评论
• 0
收藏
• 一键三连
• 扫一扫，分享海报

©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客

Jaihk662

¥2 ¥4 ¥6 ¥10 ¥20