[Noi109] line up

Subject is in fact equivalent to make larger and smaller than m the number does not exceed the number of n-/ 2 , so that when a number of the processing to be, or to change it to m , or not modified, according to this greedy to be completed .

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 #define ll long long
 4 ll n,m,k,y,ans,a[200001];
 5 int main(){
 6     scanf("%lld%lld",&n,&m);
 7     for(int i=1;i<=n;i++)scanf("%lld",&a[i]);
 8     sort(a+1,a+n+1);
 9     k=n/2+1;
10     y=0x3f3f3f3f;
11     for(int i=1;i<=n;i++)
12         if ((a[i]==m)&&(abs(y-k)>abs(k-i)))y=i;
13     if (y==0x3f3f3f3f){
14         a[n+1]=0x3f3f3f3f;
15         for(int i=0;i<=n;i++)
16             if ((a[i]<m)&&(a[i+1]>m))
17                 if (i>=k)y=i+1;
18                 else y=i;
19     }
20     for(int i=k;i<y;i++)ans+=abs(a[i]-m);
21     for(int i=y+1;i<=k;i++)ans+=abs(a[i]-m);
22     printf("%lld",ans);
23     return 0;
24 }
View Code

 

 

 

Guess you like

Origin www.cnblogs.com/PYWBKTDA/p/11272232.html