Hdu 2083 简易版之最短距离
反证法证明得到 中点 满足题意
#include<cstdio> #include<cstdlib> #include<iostream> #include<map> #include<memory.h> #include<algorithm> using namespace std; int a[600]; int ans; int main(){ int T,n,i,j; cin>>T; while(T--){ ans=0; scanf("%d",&n); for(i=1;i<=n;i++) scanf("%d",&a[i]); sort(a+1,a+n+1); int pos=(n+1)/2; for(i=1;i<=n;i++) ans+=abs(a[i]-a[pos]); printf("%d\n",ans); } return 0; }
It is your time to fight!