- #include<math.h>
- #include<iostream.h>
- #include<cstdio>
- void main()
- {
- freopen("in.txt","r",stdin);
- freopen("out.txt","w",stdout);
- short y[10000];
- short n,a,i,j,min(0);
- cin>>n;
- for(i=1;i<=n;i++)
- cin>>a>>y[i];
- bool bo;
- do
- {
- bo=true;
- for(i=n;i>1;i--)
- {
- for(j=1;j<i;j++)
- {
- if(y[j]>y[j+1])
- {
- a=y[j+1];
- y[j+1]=y[j];
- y[j]=a;
- bo=false;
- }
- }
- }
- }while(!bo);
- for(i=2;i<=n;i++) /*根据数学分析我证明了最小值一定在某个油井点上取到,而非在某两个油井点之间*/
- min+=(y[i]-y[1]);
- short dian(y[1]);
- for(i=2;i<=n;i++)
- {
- short k(0);
- for(j=1;j<=n;j++)
- k+=abs(y[j]-y[i]); /*所以只需取遍每个油井点并计算距离和即知道最小值*/
- if(min>k)
- {
- min=k;
- dian=y[i];
- }
- }
- cout<<min<<" "<<dian<<endl;
- fclose(stdin);
- fclose(stdout);
- }
- //该片段来自于http://www.codesnippet.cn/detail/0708201513386.html
来源: http://www.codesnippet.cn/detail/0708201513386.html