Bzoj 1696 [Usaco2007 feb]building A new Barn Barn Math

Source: Internet
Author: User

Test Instructions:Link Method:Math + Simulation parsing:First of all, this kind of problem is not the first time to see, so directly know to take the median of x, y median. The problem is that it is very annoying to discuss the situation. There is a limitation in the question, given that the point to be summed cannot be selected. So if an odd number of points, find the x median, y median number. Verify that x, y exists in the set of points to be summed, and four cases are judged if present. The four cases of judgement are (x-1,y), (X+1,y), (x,y-1), (x,y+1), suboptimal solution must exist in these four cases, this should be very good understanding? If it does not exist in the origin set, it is summed directly. If you have an even number of points, it is advisable to discuss all the X-values and the Y-values, and we recommend using the repulsion. As for the details, study for yourself. Code:
#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;intNintA[n];intB[n];intxx[5]={0,-1,1,0,0};intyy[5]={0,0,0,1,-1};structnode{intx, y;} C[n];intMain () {scanf("%d", &n); for(intI=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)    {inttmp=n/2+1;intTMPX=A[TMP],TMPY=B[TMP];intans=0;intflag=1; for(intI=1; i<=n;i++) {if(TMPX==C[I].X&AMP;&AMP;TMPY==C[I].Y) {flag=0; Break;} }if(flag) { for(intI=1; i<=n;i++) {ans+=ABS(Tmpx-a[i]) +ABS(Tmpy-b[i]); }printf("%d 1\n", ans); }Else{Ans=inf;intCnt=0; for(intI=1; i<=4; i++) {intX=tmpx+xx[i],y=tmpy+yy[i];intret=0; for(intj=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{inttmpx1=a[n/2],tmpx2=a[n/2+1];inttmpy1=b[n/2],tmpy2=b[n/2+1];intret=0, Cnt= (tmpx2-tmpx1+1) * (tmpy2-tmpy1+1); for(intk=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); }}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Bzoj 1696 [Usaco2007 feb]building A new Barn Barn Math

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.