Codeforces 433 C. Ryouko's Memory Note
2014-08-15 15:48
381 查看
C. Ryouko's Memory Note
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
Ryouko is an extremely forgetful girl, she could even forget something that has just happened. So in order to remember, she takes a notebook with her, called Ryouko's Memory Note. She writes what
she sees and what she hears on the notebook, and the notebook became her memory.
Though Ryouko is forgetful, she is also born with superb analyzing abilities. However, analyzing depends greatly on gathered information, in other words, memory. So she has to shuffle through her notebook whenever she needs to analyze, which is tough work.
Ryouko's notebook consists of n pages, numbered from 1 to n.
To make life (and this problem) easier, we consider that to turn from page x to page y, |x - y| pages
should be turned. During analyzing, Ryouko needs m pieces of information, the i-th
piece of information is on page ai.
Information must be read from the notebook in order, so the total number of pages that Ryouko needs to turn is
.
Ryouko wants to decrease the number of pages that need to be turned. In order to achieve this, she can merge two pages of her notebook. If Ryouko merges page x to
page y, she would copy all the information on page x to y (1 ≤ x, y ≤ n),
and consequently, all elements in sequence a that was x would
become y. Note that x can be equal to y,
in which case no changes take place.
Please tell Ryouko the minimum number of pages that she needs to turn. Note she can apply the described operation at most once before the reading. Note that the answer can exceed 32-bit integers.
Input
The first line of input contains two integers n and m (1 ≤ n, m ≤ 105).
The next line contains m integers separated by spaces: a1, a2, ..., am (1 ≤ ai ≤ n).
Output
Print a single integer — the minimum number of pages Ryouko needs to turn.
Sample test(s)
input
output
input
output
Note
In the first sample, the optimal solution is to merge page 4 to 3, after merging sequence a becomes {1, 2, 3, 3, 3, 2},
so the number of pages Ryouko needs to turn is |1 - 2| + |2 - 3| + |3 - 3| + |3 - 3| + |3 - 2| = 3.
In the second sample, optimal solution is achieved by merging page 9 to 4.
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
Ryouko is an extremely forgetful girl, she could even forget something that has just happened. So in order to remember, she takes a notebook with her, called Ryouko's Memory Note. She writes what
she sees and what she hears on the notebook, and the notebook became her memory.
Though Ryouko is forgetful, she is also born with superb analyzing abilities. However, analyzing depends greatly on gathered information, in other words, memory. So she has to shuffle through her notebook whenever she needs to analyze, which is tough work.
Ryouko's notebook consists of n pages, numbered from 1 to n.
To make life (and this problem) easier, we consider that to turn from page x to page y, |x - y| pages
should be turned. During analyzing, Ryouko needs m pieces of information, the i-th
piece of information is on page ai.
Information must be read from the notebook in order, so the total number of pages that Ryouko needs to turn is
.
Ryouko wants to decrease the number of pages that need to be turned. In order to achieve this, she can merge two pages of her notebook. If Ryouko merges page x to
page y, she would copy all the information on page x to y (1 ≤ x, y ≤ n),
and consequently, all elements in sequence a that was x would
become y. Note that x can be equal to y,
in which case no changes take place.
Please tell Ryouko the minimum number of pages that she needs to turn. Note she can apply the described operation at most once before the reading. Note that the answer can exceed 32-bit integers.
Input
The first line of input contains two integers n and m (1 ≤ n, m ≤ 105).
The next line contains m integers separated by spaces: a1, a2, ..., am (1 ≤ ai ≤ n).
Output
Print a single integer — the minimum number of pages Ryouko needs to turn.
Sample test(s)
input
4 6 1 2 3 4 3 2
output
3
input
10 5 9 4 3 8 8
output
6
Note
In the first sample, the optimal solution is to merge page 4 to 3, after merging sequence a becomes {1, 2, 3, 3, 3, 2},
so the number of pages Ryouko needs to turn is |1 - 2| + |2 - 3| + |3 - 3| + |3 - 3| + |3 - 2| = 3.
In the second sample, optimal solution is achieved by merging page 9 to 4.
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <vector> using namespace std; const int maxn=100100; typedef long long int LL; int n,m; LL a[maxn]; vector<int> near[maxn]; int main() { cin>>n>>m; for(int i=0;i<m;i++) { cin>>a[i]; } LL cur=0,orz; for(int i=0;i<m-1;i++) { cur+=abs(a[i+1]-a[i]); } orz=cur; for(int i=0;i<m;i++) { if(i-1>=0&&a[i-1]!=a[i]) { near[a[i]].push_back(a[i-1]); } if(i+1<m&&a[i]!=a[i+1]) { near[a[i]].push_back(a[i+1]); } } for(int i=1;i<=n;i++) { int sz=near[i].size(); if(sz) { sort(near[i].begin(),near[i].end()); LL change=0;int mid=near[i][sz/2]; for(int j=0;j<sz;j++) { change+=abs(near[i][j]-mid)-abs(near[i][j]-i); } cur=min(cur,orz+change); } } cout<<cur<<endl; return 0; }
相关文章推荐
- Codeforces 433 C. Ryouko's Memory Note
- CF 248 C. Ryouko's Memory Note
- Codeforces Round #248 (Div. 1)——Ryouko's Memory Note
- CF 433C Ryouko's Memory Note
- Codeforces 433 C. Ryouko's Memory Note
- Codeforces Round #248 (Div. 2) C.Ryouko's Memory Note
- Codeforces 433 C Ryouko's Memory Note
- Codeforces RoundC. Ryouko's Memory Note
- Codeforces Round #248 (Div. 2) C - Ryouko's Memory Note
- Codeforces 433 C Ryouko's Memory Note
- Note: In form builder, triggers 'execution hierarchy' caused funny issue in enter-query mode.
- Master Note:'Warning: Subscription For Node Down Event Still Pending' In Listener Log
- Note On <You Don't Know JS - Scope and Closures>
- Codeforces Round #248 (Div. 2) C. Ryouko's Memory Note
- Note on <Zend Framework - A Beginner's Guide> - 03
- arm环境下编译出现下面警告的解决办法 note: the mangling of 'va_list' has changed in GCC 4.4
- Note on <Zend Framework - A Beginner's Guide> - 05
- Note on <Zend Framework - A Beginner's Guide> - 04 使用Doctrine初体验
- C. Ryouko's Memory Note
- discuz7.2问题SQL:SELECT * FROM [Table]notelist WHERE closed='0' AND app1'-5' LIMIT 1