Codeforces Round #248 (Div. 2) C. Ryouko's Memory Note
题目链接: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的更多相关文章
- 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 (vector 替换)
题目链接 题意:给m个数字, 这些数字都不大于 n, sum的值为相邻两个数字 差的绝对值.求这n个数字里把一个数字 用 其中另一个数字代替以后, 最小的sum值. 分析:刚开始以为两个for 最坏 ...
- 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为奇数时一定 ...
随机推荐
- trap
http://blog.csdn.net/elbort/article/details/8525599 http://mywiki.wooledge.org/SignalTrap
- RRD
http://my.oschina.net/u/1458120/blog/208857
- Appium+Robotframework实现Android应用的自动化测试-5:RIDE中AppiumLibrary的配置
可能很多朋友已经迫不及待的想要用RobotFramework+AppiumLibrary来写Android App的测试脚本了,那我们也废话少说,直接开始. 首先打开RIDE,这是编写RobotFra ...
- Bitmap vs 2Bitmap的实现
[本文链接] http://www.cnblogs.com/hellogiser/p/bitmap-vs-2bitmap.html [题目] 在2.5亿个整数找出不重复的整数,内存不足以容纳着2.5亿 ...
- iOS 关于UIWindow的理解
Every iOS app has a window that handles the presentation of the app’s user interface. Although the w ...
- codeforces 582A. GCD Table 解题报告
题目链接:http://codeforces.com/problemset/problem/582/A 网上很多题解,就不说了,直接贴代码= = 官方题解: http://codeforces.com ...
- mysql格式化整数类型时间生成年月日时分秒格式(long或string接收)
数据库格式: 数据库mysql语句: FROM_UNIXTIME( s.timemodified, '%Y-%m-%d %h:%i:%s' ) 生成结果: 测试sql为: SELECT *, FROM ...
- 【leetcode】Binary Tree Right Side View(middle)
Given a binary tree, imagine yourself standing on the right side of it, return the values of the nod ...
- supersr--图片轮播逻辑
// // ViewController.m // 图片轮播 // // Created by apple on 14-5-18. // Copyright (c) 2014年 All rig ...
- springMVC创建基础变量
在springMVC中有一些变量是基础变量,可以在全局多个地方使用,在修改规则的时候,这样只用修改一个地方就好了,而且可以避免很多不必要的bug出现下面就来总结一下在我的项目中如何去创建一个全局基础变 ...