博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Codeforces Round #248 (Div. 2) C. Ryouko's Memory Note
阅读量:4496 次
发布时间:2019-06-08

本文共 1124 字,大约阅读时间需要 3 分钟。

 题目链接:

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

#include 
#include
#include
#include
#include
#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
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;}

转载于:https://www.cnblogs.com/wally/p/4477070.html

你可能感兴趣的文章
android 教程实例系列
查看>>
lucene笔记
查看>>
tomcat无法正常shutdown
查看>>
zookeeper + dubbo 搭建
查看>>
根据前序遍历和中序遍历求出二叉树并打印
查看>>
UOJ356 [JOI2017春季合宿] Port Facility 【启发式合并】【堆】【并查集】
查看>>
Delphi的命令行编译命令
查看>>
BZOJ 1901 Zju2112 Dynamic Rankings 题解
查看>>
C++虚析构函数
查看>>
《玩转.NET Micro Framework 移植-基于STM32F10x处理器》--微软中国.NET Micro Framework项目组工程师所作之序...
查看>>
php服务端搜索,功能改进
查看>>
unity, 在surface shader中访问顶点色
查看>>
Spring声明式事务配置
查看>>
并查集的实现
查看>>
Leetcode 350. Intersection of Two Arrays II
查看>>
EditPlus VC2010 and 2008 C/C++配置
查看>>
Practical Lessons from Predicting Clicks on Ads at Facebook
查看>>
JFrame面板
查看>>
Android自动化压力测试之Monkey Test 异常解读(五)
查看>>
Compressing Convolutional Neural Networks in the Frequency Domain 论文笔记
查看>>