Codeforces 433 C. Ryouko's Memory Note
1 second
256 megabytes
standard input
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.
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).
Print a single integer — the minimum number of pages Ryouko needs to turn.
4 6
1 2 3 4 3 2
3
10 5
9 4 3 8 8
6
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的更多相关文章
- Codeforces Round #248 (Div. 1)——Ryouko's Memory Note
题目连接 题意: 给n和m,一行m个1<=x<=n的数.记c=.如今仅仅能选择一个数x变成y,序列中全部等于x的值都变成y,求最小的c 分析: 对于一个数x,把与他相邻的所有的非x的数所有 ...
- Codeforces Round #248 (Div. 1) A. Ryouko's Memory Note 水题
A. Ryouko's Memory Note 题目连接: http://www.codeforces.com/contest/434/problem/A Description Ryouko is ...
- codeforces 434A A. Ryouko's Memory Note(数学)
题目链接: A. Ryouko's Memory Note time limit per test 1 second memory limit per test 256 megabytes input ...
- Codeforces Round #248 (Div. 2) C. Ryouko's Memory Note
题目链接:http://codeforces.com/contest/433/problem/C 思路:可以想到,要把某一个数字变成他的相邻中的数字的其中一个,这样总和才会减少,于是我们可以把每个数的 ...
- codeforces 433C. Ryouko's Memory Note 解题报告
题目链接:http://codeforces.com/problemset/problem/433/C 题目意思:一本书有 n 页,每页的编号依次从 1 到 n 编排.如果从页 x 翻到页 y,那么| ...
- Codeforces Round #248 (Div. 2) C. Ryouko's Memory Note (vector 替换)
题目链接 题意:给m个数字, 这些数字都不大于 n, sum的值为相邻两个数字 差的绝对值.求这n个数字里把一个数字 用 其中另一个数字代替以后, 最小的sum值. 分析:刚开始以为两个for 最坏 ...
- codeforces C. Ryouko's Memory Note
题意:给你m个数,然后你选择一个数替换成别的数,使得.最小.注意选择的那个数在这m个数与它相同的数都必须替换同样的数. 思路:用vector记录每一个数与它相邻的数,如果相同不必记录,然后遍历替换成与 ...
- CodeForces 433C Ryouko's Memory Note (中位数定理)
<题目链接> 题目大意:给你一堆数字,允许你修改所有相同的数字成为别的数字,不过只能修改一次,问你修改后序列相邻数字的距离和最小是多少. 解题分析: 首先,修改不是任意的,否则那样情况太多 ...
- Ryouko's Memory Note
题目意思:一个书有 n 页,每页的编号依次从 1 到 n 编排.如果从页 x 翻到页 y,那么|x-y|页都需要翻到(联系生活实际就很容易理解的了).接着有m pieces 的 information ...
随机推荐
- 类linux系统/proc/sysrq-trigger文件功能作用
立即重启计算机 echo "b" > /proc/sysrq-trigger 立即关闭计算机 echo "o" > /proc/ ...
- pthread_t definition
近期在看google的chromium的代码,认为其基础库base中的对于与平台有关的线程的数据结构的定义与其代码中的凝视部分不匹配. // PlatformThreadHandle should n ...
- POJ3071:Football(概率DP)
Description Consider a single-elimination football tournament involving 2n teams, denoted 1, 2, …, 2 ...
- swiftTools
String+Exten.swift // // String+Exten.swift // swiftTest // // Created by napiao on 15/11/27. // Cop ...
- cocoapods导入第三方库
1.移除现有Ruby默认源 终端:gem sources --remove https://rubygems.org/ 2.使用新的源 终端:gem sources -a https://ruby.t ...
- Android系统五大布局详解Layout
我们知道Android系统应用程序一般是由多个Activity组成,而这些Activity以视图的形式展现在我们面前, 视图都是由一个一个的组件构成的.组件就是我们常见的Button.TextEdit ...
- hdu 4612 Warm up(无向图Tarjan+树的直径)
题意:有N个点,M条边(有重边)的无向图,这样图中会可能有桥,问加一条边后,使桥最少,求该桥树. 思路:这个标准想法很好想到,缩点后,求出图中的桥的个数,然后重建图必为树,求出树的最长直径,在该直径的 ...
- 安装eclipse
前提,安装好jdk并成功配置好环境变量 下载eclipse-standard-kepler-R-win32-x86_64,直接打开里面的eclipse.exe文件即可
- Scrapy:python3下的第一次运行测试
1,引言 <Scrapy的架构初探>一文讲解了Scrapy的架构,本文就实际来安装运行一下Scrapy爬虫.本文以官网的tutorial作为例子,完整的代码可以在github上下载. 2, ...
- Ubuntu14.04安装配置SVN及Trac
还是个实习生的时候,项目管理十分欠缺,会出现很多问题,痛定思痛,决定要改变现状,养成良好的项目管理习惯,看网上工具很多,在这里尝试使用SVN作代码版本控制,使用trac作为项目管理追踪.本文采用的操作 ...