# Codeforces248 (div1) A. Ryouko's memory note

Source: Internet
Author: User

You can change the number in a sequence to minimize the sum of the absolute values of the last number and the previous Number Difference in the sequence.

Add the number adjacent to number X to the linked list of G [x] (if this value is also X, it is not added), then the number X is changed, it will only affect the number in this linked list.

To minimize the sum of the absolute values of the Number Difference between x and the Number Difference in the linked list, we can use the median of the sorted sequence.

`//#pragma comment(linker, "/STACK:102400000,102400000")//HEAD#include <cstdio>#include <cstring>#include <vector>#include <iostream>#include <algorithm>#include <queue>#include <string>#include <set>#include <stack>#include <map>#include <cmath>#include <cstdlib>#include <list>using namespace std;//LOOP#define FE(i, a, b) for(int i = (a); i <= (b); ++i)#define FED(i, b, a) for(int i = (b); i>= (a); --i)#define REP(i, N) for(int i = 0; i < (N); ++i)#define CLR(A,value) memset(A,value,sizeof(A))//STL#define PB push_back//INPUT#define RI(n) scanf("%d", &n)#define RII(n, m) scanf("%d%d", &n, &m)#define RIII(n, m, k) scanf("%d%d%d", &n, &m, &k)#define RS(s) scanf("%s", s)#define FF(i, a, b) for(int i = (a); i < (b); ++i)#define FD(i, b, a) for(int i = (b) - 1; i >= (a); --i)#define CPY(a, b) memcpy(a, b, sizeof(a))#define FC(it, c) for(__typeof((c).begin()) it = (c).begin(); it != (c).end(); it++)#define EQ(a, b) (fabs((a) - (b)) <= 1e-10)#define ALL(c) (c).begin(), (c).end()#define SZ(V) (int)V.size()#define RIV(n, m, k, p) scanf("%d%d%d%d", &n, &m, &k, &p)#define RV(n, m, k, p, q) scanf("%d%d%d%d%d", &n, &m, &k, &p, &q)#define WI(n) printf("%d\n", n)#define WS(s) printf("%s\n", s)#define sqr(x) x * xtypedef vector <int> VI;typedef unsigned long long ULL;typedef long long LL;const int INF = 0x3f3f3f3f;const int maxn = 100010;const double eps = 1e-10;const LL MOD = 1e9 + 9;int a[maxn];VI G[maxn];int main(){    int n, m;    while (~RII(n, m))    {        REP(i, n + 1)   G[i].clear();        FE(i, 1, m)           RI(a[i]);        LL tot = 0;        FE(i, 1, m)        {            if (i != 1 && a[i] != a[i - 1])                G[a[i]].push_back(a[i - 1]);            if (i != m && a[i] != a[i + 1])                G[a[i]].push_back(a[i + 1]), tot += abs(a[i + 1] - a[i]);        }        LL ans = tot;//        cout << tot << "----" <<endl;        FE(i, 1, n)        {            if (G[i].size() == 0) continue;            LL t = tot;//            cout << t1 << "  ----x:";            sort(G[i].begin(), G[i].end());            int x = G[i][G[i].size() / 2];            REP(j, G[i].size())                t += abs(G[i][j] - x) - abs(G[i][j] - i);            ans = min(ans, t);//            cout << x <<"   t2: " << t2 <<endl;        }//        if (m == 1) ans = 0;        cout << ans << endl;    }    return 0;}/*4 61 2 3 4 3 210 59 4 3 8 81000 50 1 1000 999 1*/`

Related Keywords:

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

## A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

• #### Sales Support

1 on 1 presale consultation

• #### After-Sales Support

24/7 Technical Support 6 Free Tickets per Quarter Faster Response

• Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.