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 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 Input
4 6
1 2 3 4 3 2
Sample Output
3
Hint
题意
有一个人,他在读书,然后他会依次翻页,现在他可以使用一次魔法,把可以把第x页合并在第y页上
问你至多使用一次魔法的情况下,他最少翻页多少页?
题解:
对于某一页,他如果变了的话,就只会影响要翻到他的页数,和他要翻到的页数
把这些都存下来,然后显然取个中位数就好了
所以直接这样暴力莽一波就好了
复杂度是nlogn的,均摊了一下
代码
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5+7;
int n,m,a[maxn];
vector<int>near[maxn];
int main()
{
scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++)
scanf("%d",&a[i]);
for(int i=1;i<=m;i++)
{
if(i!=1&&a[i]!=a[i-1])
near[a[i]].push_back(a[i-1]);
if(i!=m&&a[i]!=a[i+1])
near[a[i]].push_back(a[i+1]);
}
long long ans = 0;
for(int i=1;i<m;i++)
ans+=abs(a[i]-a[i+1]);
long long Ans=ans;
for(int i=1;i<=n;i++)
{
if(near[i].size()==0)continue;
long long tmp=0;
for(int j=0;j<near[i].size();j++)
tmp+=abs(near[i][j]-i);
sort(near[i].begin(),near[i].end());
long long p = near[i][near[i].size()/2];
long long tmp2=0;
for(int j=0;j<near[i].size();j++)
tmp2+=abs(near[i][j]-p);
Ans=min(ans-tmp+tmp2,Ans);
}
cout<<Ans<<endl;
}
Codeforces Round #248 (Div. 1) A. Ryouko's Memory Note 水题的更多相关文章
- Codeforces Round #248 (Div. 2) C. Ryouko's Memory Note
题目链接:http://codeforces.com/contest/433/problem/C 思路:可以想到,要把某一个数字变成他的相邻中的数字的其中一个,这样总和才会减少,于是我们可以把每个数的 ...
- Codeforces Round #248 (Div. 2) C. Ryouko's Memory Note (vector 替换)
题目链接 题意:给m个数字, 这些数字都不大于 n, sum的值为相邻两个数字 差的绝对值.求这n个数字里把一个数字 用 其中另一个数字代替以后, 最小的sum值. 分析:刚开始以为两个for 最坏 ...
- Codeforces Round #368 (Div. 2) A. Brain's Photos (水题)
Brain's Photos 题目链接: http://codeforces.com/contest/707/problem/A Description Small, but very brave, ...
- Codeforces Round #373 (Div. 2) C. Efim and Strange Grade 水题
C. Efim and Strange Grade 题目连接: http://codeforces.com/contest/719/problem/C Description Efim just re ...
- Codeforces Round #185 (Div. 2) A. Whose sentence is it? 水题
A. Whose sentence is it? Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/ ...
- Codeforces Round #373 (Div. 2) A. Vitya in the Countryside 水题
A. Vitya in the Countryside 题目连接: http://codeforces.com/contest/719/problem/A Description Every summ ...
- Codeforces Round #371 (Div. 2) A. Meeting of Old Friends 水题
A. Meeting of Old Friends 题目连接: http://codeforces.com/contest/714/problem/A Description Today an out ...
- Codeforces Round #355 (Div. 2) B. Vanya and Food Processor 水题
B. Vanya and Food Processor 题目连接: http://www.codeforces.com/contest/677/problem/B Description Vanya ...
- Codeforces Round #310 (Div. 2) B. Case of Fake Numbers 水题
B. Case of Fake Numbers Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/5 ...
随机推荐
- 关于app的cpu占用率想到的几个问题
1.top 命令获取的cpu是手机瞬间的cpu 2.dumpsys获取的是一段时间cpu的平均值?那么这段时间是指哪段,从哪开始到什么时候结束? 3.如果想测试app某操作下的cpu占用情况时候.应该 ...
- C#基础学习之FileStream
FileStream和File的区别 后者比前者给内存带来压力大. FileStream可以操作字节也就是可以保存任何类型的文件. 1.FileStream读文件操作 //OpenOrCreate: ...
- mac 升级10.12 php debug 环境 跑不起的解决 解决方案
1: mac 升级后发现 php从原来的5.5 升级为 5.6 了... 所以以前 php.ini 里面的配置全部都没有了. mac 给我们做了备份2: 没办法只能升级php对应的插件到5. ...
- css-css背景
CSS 允许应用纯色作为背景,也允许使用背景图像创建相当复杂的效果 一:背景色background-color 属性 p {background-color: gray;} 二:背景图像 backgr ...
- 使用Docker快速搭建sftp服务
一.安装docker环境 参见 http://www.cnblogs.com/rslai/p/8403350.html 二.从Docker Hub查找sftp镜像 docker search sftp ...
- java基础34 泛型的使用
本文知识点(目录): 1.泛型的好处 2.泛型在集合中的常见应用(这里只用String类型举例) 3.在泛型中是不能使用基本数据类型,如果需要使用基本数据类型,那么就使用基本数据类型对应的 ...
- Linux基础 - crontab
列出当前用户设置的定时任务 crontab -l 编辑定时任务 crontab -e 用法 m h dom mon dow * * * * * command 字段详解: *:any m: minut ...
- opencv(5)GUI
OpenCV的图形用户界面(Graphical User Interface, GUI)和绘图等相关功能也是很有用的功能,无论是可视化,图像调试还是我们这节要实现的标注任务,都可以有所帮助. 窗口循环 ...
- Introduction to MWB Minor Mode
Introduction to MWB Minor Mode */--> Table of Contents 1. Introduction 2. Usage 1 Introduction MW ...
- Visual Studio 2017 百度云下载
链接: https://pan.baidu.com/s/1kFjGwyj5HwabvmJKiyLF_g 提取码: 关注公众号[GitHubCN]回复获取 秘钥Enterprise:NJVYC-BM ...