Codeforces Round #248 (Div. 2) C. Ryouko's Memory Note (vector 替换)
题意:给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 替换)的更多相关文章
- 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 Round #248 (Div. 2) (ABCD解决问题的方法)
比赛链接:http://codeforces.com/contest/433 A. Kitahara Haruki's Gift time limit per test:1 second memory ...
- Codeforces Round #248 (Div. 1)——Ryouko's Memory Note
题目连接 题意: 给n和m,一行m个1<=x<=n的数.记c=.如今仅仅能选择一个数x变成y,序列中全部等于x的值都变成y,求最小的c 分析: 对于一个数x,把与他相邻的所有的非x的数所有 ...
- Codeforces Round #248 (Div. 2)C 题
题目:http://codeforces.com/contest/433/problem/C 没想到做法就各种纠结, 今天做的都快疯掉了, 太弱了, 等题解一出,就各种恍然大悟 不应该不应该 正文: ...
- 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 ...
- Codeforces Round #248 (Div. 2) B称号 【数据结构:树状数组】
主题链接:http://codeforces.com/contest/433/problem/B 题目大意:给n(1 ≤ n ≤ 105)个数据(1 ≤ vi ≤ 109),当中有m(1 ≤ m ≤ ...
- Codeforces Round #248 (Div. 2) B. Kuriyama Mirai's Stones
题目简单描述就是求数组中[l,r]区间的和 #include <iostream> #include <vector> #include <string> #inc ...
- Codeforces Round #248 (Div. 2) A. Kitahara Haruki's Gift
解决思路是统计100的个数为cnt1,200的个数为cnt2 则 cnt1 cnt2 奇数 奇数 奇数 偶数 偶数 奇数 偶数 偶数 当cnt1为奇数时一定 ...
随机推荐
- ubuntu 14.04链接无线路由,建立无线和有线链接
神奇的linux. 废话不多说,进入主题: 首先1:买一部带wifi的笔记本电脑,买一个可用的无线路由器,像网络提供商申请上网缴费==! 2,中国国情,我们大多都是用ADSL咯.所以其它情况就不说了. ...
- iOS刷新第三方MJRefresh的基本使用
iOS开发中最好用的刷新第三方框架 MJRefresh GitHub : https://github.com/CoderMJLee/MJRefresh UIRefreshControl的介绍 1,U ...
- Eclipse启动的时候窗口一闪就关的解决办法(转)
有时候会碰到如题这种问题,从网上查知解决办法,非常管用 为eclipse.exe创建一个快捷方式,然后快捷方式上右键-属性,在目标栏填入 E:\eclipse\eclipse.exe -vm &quo ...
- sharepoint 列表的column验证----------SharePoint 2010 List Validation Formula
首先,依次打开-站点->列表名称->列表设置->验证设置: 我们设置一个时间的列不能小于当前时间,并且在编辑的时候不需要验证. =OR([,Created<TODAY())
- [book]awesome-machine-learning books
https://github.com/josephmisiti/awesome-machine-learning/blob/master/books.md Machine-Learning / Dat ...
- Introduction To Monte Carlo Methods
Introduction To Monte Carlo Methods I’m going to keep this tutorial light on math, because the goal ...
- 消除SDK更新时的“https://dl-ssl.google.com refused”异常
原地址:http://blog.csdn.net/x605940745/article/details/17911115 消除SDK更新时的“https://dl-ssl.google.com ref ...
- 【c++基础】const、const指针、const引用
一.const常量 声明时必须同时初始化(和“引用”一样) 二.const指针 三.const引用 引用本身和引用的对象都是const对象,可以用字面值来赋给const引用(普通引用则不行) ; co ...
- 初学tornado之MVC版helloworld
作者:the5fire | 标签: MVC tornado | 发布:2012-08-06 2:41 p.m. 文接上篇,看我一个简单的helloworld,虽然觉得这个框架着实精小,但是实际开发 ...
- lua语言入门之Sublime Text设置lua的Build System
转自: http://blog.csdn.net/wangbin_jxust/article/details/8911956 最近开始学习LUA语言,使用Sublime Text作为编辑器,不得不说, ...