For the first time, the idea is: the maximum two number of places to find the difference and then the merge to calculate a value and a series of the same number to do the merge after the calculation of a value comparison to the maximum output; So I searched the original solution found that the correct way to solve the problem is: the use of mathematics in the median principle, respectively, a number of two adjacent to each other and the number of different numbers into the vector container vectors and then sort, find the median calculation once, find the maximum value, and then the value of the input sequence to calculate a sum, Then subtraction is the solution.
On the principle of median I would mention a little bit:
To find the median, first of all the data sorted (from small to large), and then calculate the median number, the data is odd and even two kinds to seek. When sorting, the same number cannot be omitted) [2]
The median is calculated to avoid extreme data, which represents the middle of the overall data.
If the total number is odd, take the middle number in the order from small to large.
If the total number is even, the average of the two numbers in the middle is taken from small to large.
The application of the median is the sum of the distance from the other elements; The ontology also involves the idea of division and treatment; Well, don't say a lot of nonsense, just put my code on it;
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <algorithm>5#include <vector>6 #defineLL __int647 using namespacestd;8 Const intN =100000+Ten;9 intA[n];Ten intn,m; One intVis[n]; Avector<int>V[n]; - intAbsintx) - { the if(x<0)return-x; - Else returnx; - } - intMain () + { - while(SCANF ("%d%d", &n,&m)! =EOF) + { ALL ans=0; atmemset (Vis,0,sizeofvis); - for(intI=1; i<=m;i++){ -scanf"%d",&a[i]); - if(i>1){ - if(a[i]!=a[i-1]){ -V[a[i]].push_back (a[i-1]); inv[a[i-1]].push_back (A[i]); - } toans+= (LL) ABS (a[i]-a[i-1]); + } - } theLL maxn=0; * if(m>1) $ for(intI=1; i<=m;i++){Panax Notoginseng if(Vis[a[i]])Continue; -vis[a[i]]=1; the sort (V[a[i]].begin (), V[a[i]].end ()); + intR=v[a[i]].size (); A if(r==0)Continue; theR/=2; + intMid=V[a[i]][r]; -LL t1=0; $LL t2=0; $ for(intj=0; J<v[a[i]].size (); j + +){ -t1+= (LL) ABS (mid-v[a[i]][j]); - } the for(intj=0; J<v[a[i]].size (); j + +){ -t2+= (LL) ABS (a[i]-v[a[i]][j]);Wuyi } theMAXN = Max (maxn,t2-t1); - } WuAns-=AXN; -printf"%i64d\n", ans); About } $ return 0; -}
Problem-433c-codeforces Problem Solving Report