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 ...
随机推荐
- HDU1242 Rescue(BFS+优先队列)
Rescue Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Subm ...
- apache FtpServer 整合spring部署
我们在项目中可能会出现这样的需求,使用ftp上传很大的文件后对需要对文件进行相应的逻辑处理,这时我们可以使用apache ftpServer来处理这段逻辑,只要我们做相应的部署和编写我们的逻辑代码,这 ...
- 使用 apache ant 轻松实现文件压缩/解压缩(转)
原文地址:http://blog.csdn.net/irvine007/article/details/6779492 maven配置ant包: <dependency> <grou ...
- Nginx学习之四-Nginx进程同步方式-自旋锁(spinlock)
自旋锁简介 Nginx框架使用了三种消息传递方式:共享内存.套接字.信号. Nginx主要使用了三种同步方式:原子操作.信号量.文件锁. 基于原子操作,nginx实现了一个自旋锁.自旋锁是一种非睡眠锁 ...
- linux内核中驱动开发常见的相似多态
#include<stdio.h> #include<stdlib.h> struct test { char name[20]; void (*func)(char *); ...
- Android系统Surface机制的SurfaceFlinger服务的线程模型分析
文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/8062945 在前面两篇文章中,我们分析了Sur ...
- linux 能访问内网,但不能访问外网?解决方案
用iptables就可以了 iptables -F iptables -t nat -F iptables -A INPUT -s -d -j ACCEPT iptables -A INPUT -d ...
- Emmet 语法探析
Emmet 语法探析 Emmet(Zen Coding)是一个能大幅度提高前端开发效率的一个工具. 大多数编辑器都支持Snippet,即存储和重用一些代码块.但是前提是:你必须先定义 这些代码块. E ...
- C#.NET面向对象(语法点)
一.继承 C#中继承的规则 1:继承是可传递的 A:B B:C 2:派生类应当是对基类的扩展.派生类可以添加新的成员,但不能除去已经继承的成员的定义. 3:构造函数和析构函数不能被继承 4:如果派 ...
- Jq自定义动画
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...