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 ...
随机推荐
- python 之datetime库学习
# -*- coding:utf-8 -*- import refrom datetime import datetime, timezone, timedelta def rec_time(): ...
- java基础61 JavaScript循环语句之while、do...while、for及for...in循环(网页知识)
本文知识点(目录): 1.while循环语句 2.do...while循环语句 3.for循环语句 4.for...in循环语句 5.附录1(with语句) 6.附录2( ...
- Oracle 函数 “自动生成订单号”
create or replace function get_request_code return varchar2 AS --函数的作用:自动生成订单号 v_mca_no mcode_apply_ ...
- 排序与相关性(Sorting and Relevance)
本文翻译自Elasticsearch官方指南的Sorting and Relevance一章的第一节. 原文地址:http://www.elastic.co/guide/en/elasticsearc ...
- 不同Linux机器之间拷贝文件
不同的Linux之间copy文件常用有3种方法: 第一种就是ftp,也就是其中一台Linux安装ftp Server,这样可以另外一台使用ftp的client程序来进行文件的copy. 第二种方法就是 ...
- [java笔记]常用的设计模式
1.单例设计模式 单例设计模式:保证一个类仅有一个实例,并提供一个访问它的全局访问点. 1)构造方法私有化 2)声明一个本类对象 3)给外部提供一个静态方法获取对象实例 例如: class Singl ...
- java 多线程总结篇1之——基本概念
1.什么是线程 进程:每个进程都有独立的代码和数据空间(进程上下文),进程间的切换会有较大的开销,一个进程包含1--n个线程.(进程是资源分配的最小单位) 线程:同一类线程共享代码和数据空间,每个线程 ...
- Maven3 生命周期与插件(笔记五)
第一节:Maven 生命周期 Maven 生命周期简介:Maven 通过插件的方式完成一系列过程来实现功能. Maven 拥有三套独立的生命周期: Clean 清理项目 Default 构建项目 Si ...
- 为通过 ATS 检测 Tomcat 完全 TLS v1.2、完全正向加密及其结果检验
2017 年起 app store 要求 app 对接的服务器支持 TLS v1.2,否则 ats 检测不予通过.有点强制推 TLS v1.2 的意味.本文介绍如何使 tomcat 强制执行 TLS ...
- 20155225 2006-2007-2 《Java程序设计》第四周学习总结
20155225 2006-2007-2 <Java程序设计>第四周学习总结 教材学习内容总结 对"是一种"语法测试几次之后,总结一句:满足"是一种" ...