You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

43 lines
1.0 KiB

2 years ago
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <set>
using namespace std;
const int INF = 0x3f3f3f3f;
const int N = 40000 + 10;
int n, ans;
// AC 263 ms
set<int> s;
/*
1.setlower_boundit
2.lower_boundset.end()>,set
*/
int main() {
//加快读入
ios::sync_with_stdio(false), cin.tie(0);
cin >> n;
//加入哨兵是个好习惯,防止查找最小数前驱,或,最大数后继时,发生边界问题
s.insert(-INF), s.insert(INF);
for (int i = 1; i <= n; i++) {
int x;
cin >> x;
auto it = s.lower_bound(x);
if (i == 1) //第一天的最小波动值为第一天的营业额 a1
ans += abs(x);
else
ans += min(abs(x - *(--it)), abs(x - *it));
// x入set
s.insert(x);
}
//输出结果
printf("%d\n", ans);
return 0;
}