Codeforces 433C Ryouko's Memory Note
2015-12-21 12:39
429 查看
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.
使用vector记录一个数两边的数字,然后把这些数字排序,选中间的那个当做修改后的值,有两个地方需要注意,一个是push的时候如果有几个连续相同的数,这些数是不能放进对应的vector中的,第二个是下边枚举时如果用m会超时,所以记录一下页数的最大值,用页数枚举。
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.
使用vector记录一个数两边的数字,然后把这些数字排序,选中间的那个当做修改后的值,有两个地方需要注意,一个是push的时候如果有几个连续相同的数,这些数是不能放进对应的vector中的,第二个是下边枚举时如果用m会超时,所以记录一下页数的最大值,用页数枚举。
#include <cstdio> #include <iostream> #include <algorithm> #include <vector> using namespace std; const int maxn = 100000 + 100; vector <int> v[maxn]; int arr[maxn]; int main(){ int n, m; long long temp = 0, _min, _beg = 0; int _max = 0; scanf("%d%d", &n, &m); for (int i = 0; i < m; i++){ scanf("%d", &arr[i]); _max = max(arr[i], _max); if (i != 0 && arr[i] != arr[i - 1]){ v[arr[i - 1]].push_back(arr[i]); v[arr[i]].push_back(arr[i - 1]); _beg += abs(arr[i] - arr[i- 1]); } } _min = temp = _beg; int rev, _size; vector <int> k; for (int i = 1; i <= _max; i++){ temp = _beg; _size = v[i].size(); if (_size == 0) continue; k = v[i]; sort(k.begin(), k.end()); rev = k[_size / 2]; for (int j = 0; j < _size; j++) temp += (abs(rev - k[j]) - abs(i - k[j])); _min = min(temp, _min); } cout << _min << endl; return 0; }
相关文章推荐
- VS2015和QT5.5项目出现的诡异错误(LNK2001和LNK09)的解决办法
- Qt自动补全(QCompleter或QListView)
- PHP5.3的编译扩展
- 搭建spring框架
- 别人珍惜了你,你也应该去珍惜别人。
- unity 自带样式名称(string to GUIStyle)
- FU-A方式分包
- YII配置mysql读写分离
- 公钥和私钥的理解
- Centos修炼----->Centos7办公环境打造(No2--访问ntfs文件系统)
- 公钥和私钥的理解
- 移动平台搜索应用竞争分析
- Unity中实现全局管理类的几种方式
- Sublime Text 2配置U3D完美运行环境
- GPG基本命令
- 'library cache lock' Waits: Causes and Solutions
- Linux 字符设备驱动模型
- linux用户管理及文件权限
- 真是O(1)吗?想清楚了没?
- Unity5版本雾效果问题