Notes (note)

Source: Internet
Author: User

Notes
"Problem description"
Given a sequence of length m, the subscript number is 1~m. Each element of the sequence is 1~n.
Integer. The cost of defining a sequence is

M?1

∑|ai+1-ai|

I=1

You can now select two numbers x and Y, and change all x in sequence a to Y. X can be equal to Y.
The minimum possible cost of requesting a sequence.
"Input Format"
Enter the first line containing two integers n and M. The second line contains a m-space-delimited integer, which represents the ordinal
Column A.
"Output Format"
The output line contains an integer that represents the minimum cost of the sequence.
"Sample Input 1"
4 6
1 2 3 4 3 2
"Sample Output 1"
3
"Sample Input 2"
10 5
9 4 3) 8 8
"Sample Output 1"
6
"Sample Interpretation"
In Example 1, the optimal strategy is to change 4 to 3.

In Example 2, the optimal strategy is to change 9 to 4.
"Data size and conventions"
For 30% of data, n,m<=100.
For 60% of data, n,m≤2000.
For 100% of data, 1≤n,m≤100,000.

Violence can be over 6 points, which is 30 points.

The procedure is to find nearby elements and record contribution values.

Code out of the way

#include <cstdio>#include<iostream>#include<algorithm>#include<ios>#include<vector>using namespaceStd;typedefLong Longll;Const intN = (int) 1e5;intN, m, A[n +1];vector<int> B[n +1];intMainintargcChar*argv[]) {//freopen ("note.in", "R", stdin);//freopen ("Note.out", "w", stdout);iOS:: Sync_with_stdio (false);//let Cin run faster. (Don't use it easily)CIN >> N >>m;  for(inti =1; I <= m; ++i) Cin >>A[i];  for(inti =1; I <= m; ++i) {if(I >1&& A[i-1]! = A[i]) b[a[i-1]].push_back (A[i]); if(I < m && A[i +1]! = A[i]) B[a[i +1]].push_back (A[i]); }//find the nearby element. ll ans= 0LL, sum =0LL;  for(inti =1; I <= N; ++i) {if(!b[i].size ())Continue;        Sort (B[i].begin (), B[i].end ()); inty = b[i][b[i].size () >>1]; ll before= 0LL, after =0LL;  for(intj =0; J < B[i].size (); ++j) {before+ = ABS (I-B[i][j]); after+ = ABS (Y-B[i][j]); } ans= Max (ans, before-after), sum + =before; } cout<< sum/2-Ans << Endl;//because the two sides are added, so/2. fclose (stdin);    Fclose (stdout); return 0;}
View Code

Notes (note)

Contact Us

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.