Ryouko's Memory Note
使用vector记录一个数两边的数字,然后把这些数字排序,选中间的那个当做修改后的值.
#include<stdio.h> #include<algorithm> #include<vector> using namespace std; const int MAX = 100100; int a[MAX]; vector<int>ne[MAX];//相邻页 int fabs(int x) { if(x<0)x=-x; return x; } int main() { int n,m; int i,j; int tmp1,tmp2; long long sum1,sum2,maxdis; long long ans; while(scanf("%d %d",&n,&m)!=EOF) { for(i=0;i<m;i++) scanf("%d",&a[i]); for(i=0;i<m-1;i++)//statistics { if(a[i]!=a[i+1]) { ne[a[i]].push_back(a[i+1]); ne[a[i+1]].push_back(a[i]); } } maxdis = 0; ans=0; for(i=1;i<=n;i++)//sort { if(!ne[i].empty()) { sort(ne[i].begin(),ne[i].end()); tmp2 = ne[i].size()/2; tmp1 = ne[i][tmp2]; sum1=0; sum2=0; for(j=0;j<ne[i].size();j++) { sum1+=fabs(ne[i][j]-tmp1); sum2+=fabs(ne[i][j]-i); } maxdis = max(maxdis,sum2-sum1); ans+=sum2;// total turn page former } } printf("%I64d\n",ans/2-maxdis); for(i=1;i<=n;i++) ne[i].clear(); } return 0; }