codeforces 434A A. 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 题意: a[i]表示a[i]页,按顺序翻页,现在可以把其中一页的内容全部复制到另一页,问最少翻的页数; 思路: 假设把第x页的内容复制走,|a[i]-x|+|a[j]-x|+...+|a[k]-x|,排序后去掉绝对值得x-a[i]+x-a[j]+...+a[k]-x;把x变成a[i],a[j],...a[k]的中位数就可以得到最小值;
其中a[i],a[j]...a[k]这些数是与x相邻的的页数,有个wa点就是要把一开始相邻的相同的页数合并,因为这个wa了好多发; AC代码:
#include <bits/stdc++.h>
/*#include <vector>
#include <iostream>
#include <queue>
#include <cmath>
#include <map>
#include <cstring>
#include <algorithm>
#include <cstdio>
*/
using namespace std;
#define Riep(n) for(int i=1;i<=n;i++)
#define Riop(n) for(int i=0;i<n;i++)
#define Rjep(n) for(int j=1;j<=n;j++)
#define Rjop(n) for(int j=0;j<n;j++)
#define mst(ss,b) memset(ss,b,sizeof(ss));
typedef long long LL;
template<class T> void read(T&num) {
char CH; bool F=false;
for(CH=getchar();CH<''||CH>'';F= CH=='-',CH=getchar());
for(num=;CH>=''&&CH<='';num=num*+CH-'',CH=getchar());
F && (num=-num);
}
int stk[], tp;
template<class T> inline void print(T p) {
if(!p) { puts(""); return; }
while(p) stk[++ tp] = p%, p/=;
while(tp) putchar(stk[tp--] + '');
putchar('\n');
} const LL mod=1e9+;
const double PI=acos(-1.0);
const LL inf=1e14;
const int N=1e5+; int n,m;
int a[N],vis[N],c[N];
LL b[N],sum=;
vector<int>ve[N]; int main()
{
read(n);read(m);
int cnt=;
Riep(m)
{
read(c[i]);
if(c[i]!=c[i-])a[++cnt]=c[i],vis[c[i]]++;
}
m=cnt;
for(int i=;i<=m;i++)sum=sum+abs(a[i]-a[i-]); Riep(m)
{
if(i==)ve[a[i]].push_back(a[]),b[a[i]]=b[a[i]]+abs(a[]-a[]);
else if(i==m)ve[a[i]].push_back(a[m-]),b[a[i]]=b[a[i]]+abs(a[m]-a[m-]);
else ve[a[i]].push_back(a[i-]),ve[a[i]].push_back(a[i+]),b[a[i]]=b[a[i]]+abs(a[i]-a[i-])+abs(a[i]-a[i+]);
}
LL ans=sum;
if(m>)
for(int i=;i<=n;i++)
{
if(vis[i])
{
sort(ve[i].begin(),ve[i].end());
int len=ve[i].size();
LL s=;
for(int j=;j<len;j++)
{
s=s+abs(ve[i][j]-ve[i][len/]);
}
ans=min(ans,sum-b[i]+s);
}
}
cout<<ans<<"\n";
return ;
}
codeforces 434A A. Ryouko's Memory Note(数学)的更多相关文章
- 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 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 ...
 - Codeforces 433 C. Ryouko's Memory Note
		
C. Ryouko's Memory Note time limit per test 1 second memory limit per test 256 megabytes input stand ...
 - CodeForces 433C  Ryouko's Memory Note-暴力
		
Ryouko's Memory Note Time Limit:1000MS Memory Limit:262 ...
 
随机推荐
- java使用org.apache.poi读取与保存EXCEL文件
			
一.读EXCEL文件 package com.ruijie.wis.cloud.utils; import java.io.FileInputStream; import java.io.FileNo ...
 - c#实现word,winWordControl 文档不允许复制、粘贴、隐藏工具栏、快捷保存
			
1.隐藏工具栏 //隐藏工具栏 ; i <= winWordControl1.document.CommandBars.Count; i++) { winWordControl1.documen ...
 - (剑指Offer)面试题35:第一个只出现一次的字符
			
题目: 在字符串中找出第一个只出现1次的字符,如输入“abaccdeff”,则输出b. 思路: 1.暴力遍历 从头开始扫描字符串中的每个字符,当访问某个字符时,取该字符与后面的每个字符相比较,如果没有 ...
 - 负载均衡LVS集群详解
			
一.LB--负载均衡 在负载均衡集群中需要一个分发器,我们将其称之为Director,它位于多台服务器的上面的中间层,根据内部锁定义的规则或调度方式从下面的服务器群中选择一个以此来进行响应请求,而其 ...
 - unicode 编码在线转换--javascript
			
// unicode 编码在线转换工具--javascript 本人在网上搜索,看到有使用javascript做unicode编码转换的,感觉很好玩,所以拿来使用的. 这个功能有目前测试了两种: 1) ...
 - Request对象的主要方法
			
setAttribute(String name,Object):设置名字为name的request的參数值 getAttribute(String name):返回由name指定的属性值 getAt ...
 - 百度语音识别REST API——通过使用Http网络请求方式获得语音识别功能
			
百度语音识别通过REST API的方式给开发人员提供一个通用的HTTP接口,基于该接口,开发人员能够轻松的获取语音识别能力,本文档描写叙述了使用语音识别服务REST API的方法. 长处: 较之开发人 ...
 - Codeforces Gym 100231G Voracious Steve 记忆化搜索
			
Voracious Steve 题目连接: http://codeforces.com/gym/100231/attachments Description 有两个人在玩一个游戏 有一个盆子里面有n个 ...
 - Codeforces Round #335 (Div. 2) B. Testing Robots 水题
			
B. Testing Robots Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://www.codeforces.com/contest/606 ...
 - Eclipse使用jre的原理与配置
			
近期要配置Eclipse环境,Mark当中的一些方法. 下载Eclipse SDK之后我们就要关联JRE,由于Eclipse启动须要JRE. Eclipse启动时寻找JRE的顺序: 1.假设eclip ...