【链接】 我是链接,点我呀:)

【题意】

在这里输入题意

【题解】

每天的最小波动值指的是和之前所有天的差值的绝对值中的最小值。

用set.的lower_bound函数。

每次找和他差值最小的数字就好。

(不要用lower_bound(myset.begin(),myset.end(),x)

这个比较慢

而应该用myset.lower_bound(x)

就能ac了

【代码】

#include <cstdio>
#include <algorithm>
#include <cmath>
#include <set>
using namespace std; int n;
long long ans = 0;
set<int> myset; int main(){
//freopen("D:\\rush.txt","r",stdin);
scanf("%d",&n);
for (int i = 1;i <= n;i++){
int x;
scanf("%d",&x);
if (i==1) ans = x;else{
int temp = -1; set<int>::iterator pos = myset.lower_bound(x);
int temp1 = (*pos); temp1 = abs(temp1-x);
if (pos!=myset.end()) temp = temp1;
if (pos!=myset.begin()){
pos--;
temp1 = (*pos);
temp1 = abs(temp1-x);
if (temp==-1)
temp = temp1;
else
temp = min(temp1,temp);
}
ans+=temp;
}
myset.insert(x);
}
printf("%lld\n",ans);
return 0;
}

最新文章

  1. vscode常用设置
  2. CentOS下安装hadoop
  3. JAVA开发相关
  4. 关于Xcode7中添加不了libresolv.dylib等类似库的问题
  5. Spring之LoadTimeWeaver——一个需求引发的思考---转
  6. 关于iOS去除数组中重复数据的几种方法
  7. Intent详解
  8. 转:MIME(Multipurpose Internet Mail Extensions)类型
  9. Linux内核学习方法
  10. parsing html in asp.net
  11. Linux就是这个范儿之第一次亲密接触(3)
  12. php多线程即时通讯
  13. Java的虚方法
  14. PM意识升级2.0
  15. keras 入门整理 如何shuffle,如何使用fit_generator 整理合集
  16. await异步的,容易理解一点
  17. 搭建django
  18. centos7环境下开启指定端口
  19. Cassandra创建第一个用户
  20. javascript获取id元素

热门文章

  1. Docker-镜像的操作命令
  2. redis 篇 - hash
  3. [terry笔记]python购物程序
  4. java中的hachcode方法
  5. [ReactVR] Animate Text, Images, Views, and 3D Elements Using the Animated Library in React VR
  6. 【MongoDB】深入了解MongoDB不可不知的十点
  7. 无法连接虚拟设备 ide1:0,因为主机上没有相应的设备。您要在每次开启此虚拟机时都尝试连接此虚拟设备吗?
  8. WIN32常用
  9. FastJSON杂项
  10. KafkaZookeeper2-ZookeeperClient