codeforces C. Ryouko's Memory Note
题意:给你m个数,然后你选择一个数替换成别的数,使得
.最小。注意选择的那个数在这m个数与它相同的数都必须替换同样的数。
思路:用vector记录每一个数与它相邻的数,如果相同不必记录,然后遍历替换成与它相邻的多个数的中位数之后的所有数的和取最小就可以。。
#include <cstdio>
#include <iostream>
#include <cstring>
#include <vector>
#include <algorithm>
#include <cmath>
#define maxn 100010
#define ll __int64
using namespace std;
const int mod=;
const int inf=<<; int n,m;
ll a[maxn];
int sum[maxn];
int p[maxn];
vector<int>g[maxn]; int main()
{
cin>>n>>m;
ll sum=,max1=;
for(int i=; i<=m; i++)
{
scanf("%I64d",&a[i]);
max1=max(max1,a[i]);
if(i==)continue;
if(a[i]!=a[i-])
{
g[a[i-]].push_back(a[i]);
g[a[i]].push_back(a[i-]);
sum+=abs(a[i]-a[i-]);
}
}
ll ans=sum;
for(int i=; i<=max1; i++)
{
ll tem=sum;
if(!g[i].size()) continue;
sort(g[i].begin(),g[i].end());
int xx=g[i][g[i].size()/];
for(int j=; j<(int)g[i].size(); j++)
{
tem+=(abs(xx-g[i][j])-abs(i-g[i][j]));
}
ans=min(ans,tem);
}
printf("%I64d\n",ans);
return ;
}
codeforces C. Ryouko's Memory Note的更多相关文章
- codeforces 433C. Ryouko's Memory Note 解题报告
题目链接:http://codeforces.com/problemset/problem/433/C 题目意思:一本书有 n 页,每页的编号依次从 1 到 n 编排.如果从页 x 翻到页 y,那么| ...
- CodeForces 433C 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 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 433C Ryouko's Memory Note-暴力
Ryouko's Memory Note Time Limit:1000MS Memory Limit:262 ...
- 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 最坏 ...
- 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 ...
随机推荐
- css 权威指南笔记(二)元素
替换元素 用来替换元素内容的部分并非有文档内容直接表示. img input 非替换元素 其内容由用户代理(通常是一个浏览器)在元素本身生成的框中显示. 块级元素 块级元素生成一个 元素框,(默认)会 ...
- 关于oracle的函数,存储过程,触发器,序列,视图,左右连接一些的应用 带案例
CREATE TABLE STUDENT( --创建学生表 ID NUMBER(10) PRIMARY KEY, --主键ID NAME VARCHAR2(20), CLASSNAME VA ...
- PL/SQL 触发器简介
与公司同事交流了一下,得知触发器很少用.性能是一方面,主要是如果用太多触发器,可能到时你都不知道会有什么操作自动发生. 有些操作可以在程序中控制.例如在插入某个表时,写个log表的记录.这可以用触发器 ...
- (转)function($){}(window.jQuery) 是什么意思?
function(){}(); (function(){})(); 这两个是self-invoking anonymous 自调匿名函数,用这类的方法,能强制使匿名函数成为表达式,把不合法变成合法. ...
- 调优系列-tomcat调优
http://www.360doc.com/content/14/1208/13/16070877_431273418.shtml 使用JMeter对Tomcat进行压力测试与Tomcat性能调优 n ...
- angularjs-ngTable select filter
jsp <td title="'Status'" filter="{status: 'select'}" filter-data="fn.sta ...
- clientX 属性.
Syntax: event.clientX The clientX event attribute returns the horizontal coordinate (according to th ...
- The windows PowerShell snap-in 'Microsoft.Crm.PowerShell' is not installed on this computer
加载PowerShell插件时出现以下错误: The windows PowerShell snap-in 'Microsoft.Crm.PowerShell' is not installed on ...
- SQL SERVER 查看死锁的存储过程
end
- Struts2默认拦截器配置
http://blog.csdn.net/axin66ok/article/details/7321430