题目链接:http://codeforces.com/contest/433/problem/C

思路:可以想到,要把某一个数字变成他的相邻中的数字的其中一个,这样总和才会减少,于是我们可以把每个数的左右两个相邻的数字存起来,然后我们可以想到,把某个数变成这些相邻的数的中位数总和最小。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#define REP(i, a, b) for (int i = (a); i < (b); ++i)
#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
using namespace std; const int MAX_N = (100000 + 100);
int N, M, a[MAX_N];
long long sum, ans;
vector<int > neighbor[MAX_N]; int main()
{
while (cin >> N >> M) {
FOR(i, 1, N) neighbor[i].clear();
FOR(i, 1, M) cin >> a[i];
sum = 0;
FOR(i, 2, M) if (a[i - 1] != a[i]) {
neighbor[a[i - 1]].push_back(a[i]);
neighbor[a[i]].push_back(a[i - 1]);
sum += abs(a[i - 1] - a[i]);
}
ans = sum;
FOR(i, 1, N) if ((int)neighbor[i].size()) {
sort(neighbor[i].begin(), neighbor[i].end());
int _size = (int)neighbor[i].size();
long long tmp = sum;
int target = neighbor[i][_size / 2];
REP(j, 0, _size) {
tmp = tmp - abs(i - neighbor[i][j]) + abs(target - neighbor[i][j]);
}
ans = min(ans, tmp);
}
cout << ans << endl;
}
return 0;
}

Codeforces Round #248 (Div. 2) C. Ryouko's Memory Note的更多相关文章

  1. 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 ...

  2. Codeforces Round #248 (Div. 2) C. Ryouko's Memory Note (vector 替换)

    题目链接 题意:给m个数字, 这些数字都不大于 n,  sum的值为相邻两个数字 差的绝对值.求这n个数字里把一个数字 用 其中另一个数字代替以后, 最小的sum值. 分析:刚开始以为两个for 最坏 ...

  3. Codeforces Round #248 (Div. 2) (ABCD解决问题的方法)

    比赛链接:http://codeforces.com/contest/433 A. Kitahara Haruki's Gift time limit per test:1 second memory ...

  4. Codeforces Round #248 (Div. 1)——Ryouko&#39;s Memory Note

    题目连接 题意: 给n和m,一行m个1<=x<=n的数.记c=.如今仅仅能选择一个数x变成y,序列中全部等于x的值都变成y,求最小的c 分析: 对于一个数x,把与他相邻的所有的非x的数所有 ...

  5. Codeforces Round #248 (Div. 2)C 题

    题目:http://codeforces.com/contest/433/problem/C 没想到做法就各种纠结, 今天做的都快疯掉了, 太弱了, 等题解一出,就各种恍然大悟 不应该不应该 正文: ...

  6. Codeforces Round #248 (Div. 1) B. Nanami's Digital Board 暴力 前缀和

    B. Nanami's Digital Board 题目连接: http://www.codeforces.com/contest/434/problem/B Description Nanami i ...

  7. Codeforces Round #248 (Div. 2) B称号 【数据结构:树状数组】

    主题链接:http://codeforces.com/contest/433/problem/B 题目大意:给n(1 ≤ n ≤ 105)个数据(1 ≤ vi ≤ 109),当中有m(1 ≤ m ≤  ...

  8. Codeforces Round #248 (Div. 2) B. Kuriyama Mirai's Stones

    题目简单描述就是求数组中[l,r]区间的和 #include <iostream> #include <vector> #include <string> #inc ...

  9. Codeforces Round #248 (Div. 2) A. Kitahara Haruki's Gift

    解决思路是统计100的个数为cnt1,200的个数为cnt2 则 cnt1    cnt2 奇数      奇数 奇数      偶数 偶数      奇数 偶数     偶数 当cnt1为奇数时一定 ...

随机推荐

  1. 【leetcode】Excel Sheet Column Title

    Excel Sheet Column Title Given a non-zero positive integer, return its corresponding column title as ...

  2. linux shell 中 printf 与 echo的区别

         echo echo是非常常用的shell命令.参数如下: -e:打开反斜杠字符backslash-escaped的解析,即对/n,/t等字符进行解析,而不视之为两个字符 -E:关闭反斜杠字符 ...

  3. strcpy vs memcpy

    [本文连接] http://www.cnblogs.com/hellogiser/p/strcpy_vs_memcpy.html [分析] strcpy和memcpy都是标准C库函数,它们有下面的特点 ...

  4. Oracle10g下载地址--多平台下的32位和64位

    前段时间ORACLE把10G的下载从官网拿掉了 ,许多童鞋不知道ORACLE 10g 的下载地址,这里我附上oracle 10g 下载的链接,方便大家下载.      点击链接使用迅雷即可下载.    ...

  5. SQL键值约束、索引使用

    添加約束的方式: [exec sp_helpconstraint 表名]->可用于查找到表创建的约束 CREATE TABLE stuInfo ( stuName ) NOT NULL,非空約束 ...

  6. 【leetcode】Longest Consecutive Sequence(hard)☆

    Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...

  7. php策略模式的研究

    <?php abstract class Moshi{     private $num;     public $price;     const Ted=1;     const Sed=2 ...

  8. QtCreator 添加第三方头文件库文件路径

    打开工程名.pro文件 添加 INCLUDEPATH += $$PWD/../../Obelisk/thirdparty/prebuilt/include/LeapSDKOrion LIBS += - ...

  9. 阿里云CentOS配置全过程

    1. 安装基本依赖包    yum install gcc gcc-c++ autoconf automake 2. 升级所有 yum update 3.安装mongodb 1. 配置mongodb- ...

  10. HttpClient 3.X 4.3 4.x超时设置

    HttpClient 4.3.HttpClient这货和Lucene一样,每个版本的API都变化很大,这有点让人头疼.就好比创建一个HttpClient对象吧,每一个版本的都不一样, 3.X是这样的 ...