# Bzoj 1696: [Usaco2007 Feb]Building A New Barn新牛舍 中位数,数学

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

Time Limit: 5 Sec  Memory Limit: 64 MB
Submit: 394  Solved: 181
[Submit][Status][Discuss]

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

10 4

## Source

Gold

1 #include<bits/stdc++.h>
2 using namespace std;
3 #define INF 1e9
4 int x[10010],y[10010],xx[10010],yy[10010];
5 int fx[5]={0,0,1,-1};
6 int fy[5]={1,-1,0,0};
8 {
9     int s=0,fh=1;char ch=getchar();
10     while(ch<'0'||ch>'9'){if(ch=='-')fh=-1;ch=getchar();}
11     while(ch>='0'&&ch<='9'){s=s*10+(ch-'0');ch=getchar();}
12     return s*fh;
13 }
14 int main()
15 {
16     freopen("newbarn.in","r",stdin);
17     freopen("newbarn.out","w",stdout);
18     int n,i,nn,x1,y1,x2,y2,sum,j,gs,mn;
21     sort(xx+1,xx+n+1);
22     sort(yy+1,yy+n+1);
23     if(n%2!=0)
24     {
25         nn=(n+1)/2;
26         x1=xx[nn];
27         y1=yy[nn];
28         for(j=1;j<=n;j++)if(x[j]==x1&&y[j]==y1)break;
29         if(j<=n)
30         {
31             mn=INF;gs=0;
32             for(i=0;i<=3;i++)
33             {
34                 x2=x1+fx[i];
35                 y2=y1+fy[i];
36                 sum=0;
37                 for(j=1;j<=n;j++)
38                 {
39                     sum+=(int)fabs(x[j]-x2)+(int)fabs(y[j]-y2);
40                 }
41                 if(sum<mn)
42                 {
43                     mn=sum;gs=1;
44                 }
45                 else if(sum==mn)gs++;
46             }
47             printf("%d %d",mn,gs);
48             return 0;
49         }
50         sum=0;
51         for(i=1;i<=n;i++)
52         {
53             sum+=(int)fabs(x[i]-x1)+(int)fabs(y[i]-y1);
54         }
55         printf("%d 1",sum);
56     }
57     else
58     {
59         nn=n/2;
60         sum=0;
61         x1=xx[nn];
62         y1=yy[nn];
63         sum=0;
64         for(i=1;i<=n;i++)
65         {
66             sum+=(int)fabs(x[i]-x1)+(int)fabs(y[i]-y1);
67         }
68         x2=xx[nn+1];
69         y2=yy[nn+1];
70         gs=(x2-x1+1)*(y2-y1+1);
71         for(i=1;i<=n;i++)
72         {
73             if(x[i]>=x1&&x[i]<=x2&&y[i]>=y1&&y[i]<=y2)
74             {
75                 gs--;
76             }
77         }
78         printf("%d %d",sum,gs);
79     }
80     fclose(stdin);
81     fclose(stdout);
82     return 0;
83 }
View Code

posted @ 2016-03-15 19:47  微弱的世界  阅读(199)  评论(0编辑  收藏  举报