题目链接

题意:给m个数字, 这些数字都不大于 n,  sum的值为相邻两个数字 差的绝对值。求这n个数字里把一个数字 用 其中另一个数字代替以后,

最小的sum值。

分析:刚开始以为两个for 最坏情况下 会超时,但是实际不会,因为如果第一个for循环多的话,第二个for循环肯定少。

替换的时候,用这个数相关联的排序后 中间的一个数替换,是最小的。

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <vector>
#include <algorithm>
using namespace std;
#define Min(a,b)((a)>(b)?(b):(a))
const int maxn = + ;
int a[maxn];
vector<int>v[maxn]; int main()
{
int n, m, i, j, _size, tmp;
__int64 sum, sum2, sum3;
scanf("%d%d", &n, &m);
sum = ;
for(i = ; i < m; i++)
{
scanf("%d", &a[i]);
if(i != && a[i] != a[i-])
{
v[a[i]].push_back(a[i-]);
v[a[i-]].push_back(a[i]);
sum += abs(a[i] - a[i-]);
}
}
vector<int>x;
sum3 = sum;
for(i = ; i <= n; i++)
{
_size = v[i].size();
if(_size == )
continue;
x = v[i];
sort(x.begin(), x.end());
tmp = x[_size/]; sum2 = sum3;
for(j = ; j < _size; j++)
sum2 += (abs(tmp-x[j]) - abs(i-x[j]));
sum = Min(sum, sum2);
}
printf("%I64d\n", sum);
return ;
}

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

  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

    题目链接:http://codeforces.com/contest/433/problem/C 思路:可以想到,要把某一个数字变成他的相邻中的数字的其中一个,这样总和才会减少,于是我们可以把每个数的 ...

  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. github pages

    http://zyip.github.io/facemaker/index echo "hello world" >>hello.htm git init git ad ...

  2. android开发 更新升级安装到一半自动闪退

    如题:android开发 更新升级安装到一半自动闪退,,,解决办法,如下(红色为我新增的代码) /**     * 安装APK文件     */    private void installApk( ...

  3. CSS:在input、pre中左边加上一个图标(一行和多行)

    前言 接触过EasyUI的朋友都知道其警告框就是左边有个三角警告图标,此文所做的效果正是这样.此外,还将示例多行的做法. 一.在input左边加上一个图标(一行) 注:left center定义了图标 ...

  4. oracle——merge

      一.概述 使用merge声明从一个或者更多个表或视图中筛选记录,以用来更新或者插入到一个表或视图中.你可以指定条件以决定是执行update操作还是insert操作到目标表或视图中. 这个声明是一个 ...

  5. 清除HTML中的特殊字符

    /// <summary>        /// 清楚HTML中的特殊字符        /// </summary>        /// <param name=&q ...

  6. uva 757

    贪心  优先队列 #include <cstdio> #include <cstdlib> #include <cmath> #include <map> ...

  7. Chp12: Testing

    What the Interviewer is Looking for: Big Picture Understanding Knowing How the Pieces Fit Together O ...

  8. SilverLight页面跳转(转载)

    // Silverlight页面的跳转 // (Application.Current.RootVisualasIContent).Content=newDragControl(); //Silver ...

  9. POJ1276Cash Machine

    http://poj.org/problem?id=1276 题意 : 给你一个目标钱数,再给你钱币的种数和钱币的面值,让你用这些钱凑出不大于目标钱数的钱然后输出这个最接近且不大于目标钱数的钱. 思路 ...

  10. POJ2002Squares

    http://poj.org/problem?id=2002 题意 : 就是给你很多点的坐标,任取四个,看能组成多少个不同的正方形,相同的四个点,不同顺序构成的正方形视为同一正方形. 思路 : 就是一 ...