Hello大家好!很高兴我们又见面啦!给生活添点passion,开始今天的编程之路!
我的博客:<但凡.
我的专栏:《编程之路》、《数据结构与算法之美》、《题海拾贝》
欢迎点赞,关注!
1、题目
题目链接: [HNOI2002] 营业额统计 - 洛谷
2、题解
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<set>
#include<cmath>
using namespace std;
const long long INF = 1e7 + 10;
int main()
{
int n = 0;
int day = 0;
cin >> day;
cin >> n;
set<int> mp;
mp.insert(n);
//第一天的最小波动之为第一天的营业额
int ret = n;//最小波动值的和
//错误示范 因为这一天只能和这一天之前的天数比,所以不能先都插入
//for (int i = 2;i <= day;i++)
//{
// //读入剩下的值
// int temp;
// cin >> temp;
// mp.insert(temp);
//}
//左右护法——处理边界问题
//插入 负无穷 和 正无穷
mp.insert(-INF);
mp.insert(INF);
for (int i=2;i<=day;i++)
{
int x = 0;
cin >> x;
auto it = mp.lower_bound(x);//迭代器指向元素大于等于e
auto tem = it;
tem--;//迭代器指向元素小于e
ret += min(abs(*it - x),abs(*tem - x));
//别忘了 插入x
mp.insert(x);
}
cout << ret << endl;
return 0;
}
好了,今天的内容就分享到这,我们下期再见!