Codeforces Round #248 (Div. 2) C. Ryouko's Memory Note
2014-05-30 16:43
288 查看
题目链接: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;
}
思路:可以想到,要把某一个数字变成他的相邻中的数字的其中一个,这样总和才会减少,于是我们可以把每个数的左右两个相邻的数字存起来,然后我们可以想到,把某个数变成这些相邻的数的中位数总和最小。
#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. 2) C. Ryouko's Memory Note
- Codeforces RoundC. Ryouko's Memory Note
- Codeforces Round #248 (Div. 2)
- Educational Codeforces Round 48 (Rated for Div. 2)——A. Death Note ##
- Codeforces Round #248 (Div. 2) —— B
- Codeforces Round #248 (Div. 2) B称号 【数据结构:树状数组】
- Codeforces Round #280 (Div. 2) D. Vanya and Computer Game 数学+预处理
- Codeforces Round #367 (Div. 2) E 十字链表
- Codeforces Round #FF (Div. 2)C - DZY Loves Sequences
- Codeforces Round #327 (Div. 2)
- 【Codeforces Round 345 (Div 1) B】【暴力 双指针】Image Preview 看照片、阅读、移动、反转最多看照片数
- Codeforces Round #FF (Div. 2) 447A - DZY Loves Hash(模拟)
- Codeforces Round #313 (Div. 2) 解题报告
- Codeforces Round #192 (Div. 2) C. Purification
- Codeforces Round #FF (Div. 2)A. DZY Loves Hash
- 【Codeforces Round 323 (Div 2)A】【水题】Asphalting Roads 行列之进行首次操作
- Codeforces Round #430 (Div. 2) C. Ilya And The Tree dfs+set
- Codeforces Round #192 (Div. 2)-B. Road Construction
- Codeforces Round #430 (Div. 2) D.Vitya and Strange Lesson 异或 01字典树补集最小