【题目大意】
[借用别人的概括]给出一个n个数的数列a,对于第i个元素ai定义fi=min(abs(ai-aj)),(1<=j<i),其中f1=a1。输出sum(fi) (1<=i<=n)
【思路】
平衡树,易知绝对值最小的一定是前缀和后继。要注意的是由于ai和aj可能相等,即min可能等于零,要加一个判断:如果当前平衡树中的key等于要找的值,并且当前结点的cnt大于一个,则说明之前有一个值和当前相等。另外有些情况下当前要找的结点可能没有前驱或者后继,需要判断一下。

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
const int INF=0x7fffffff;
struct treap
{
int key;
int priority;
treap* lson;
treap* rson;
int cnt;
treap()
{
key=;
cnt=;
/*这里初始值为1*/
priority=rand();
lson=rson=NULL;
}
};
treap* root=NULL; void RightRotate(treap* &rt)
{
treap* tmp=rt->lson;
rt->lson=tmp->rson;
tmp->rson=rt;
rt=tmp;
} void LeftRotate(treap* &rt)
{
treap *tmp=rt->rson;
rt->rson=tmp->lson;
tmp->lson=rt;
rt=tmp;
} void insert(treap* &rt,int x)
{
if (rt==NULL)
{
rt=new treap;
rt->key=x;
}
else
{
if (rt->key==x)
{
rt->cnt++;
}
else if (rt->key>x)
{
insert(rt->lson,x);
if (rt->lson->priority>rt->priority) RightRotate(rt);
}
else
{
insert(rt->rson,x);
if (rt->rson->priority>rt->priority) LeftRotate(rt);
}
}
} int pre(treap* &rt,int x)
{
int ans=-INF;
treap* tmp=rt;
while (tmp)
{
if (tmp->key==x && tmp->cnt>) return x; //【Attention】这句话非常重要,千万不要漏掉了!
if (tmp->key<x)
{
ans=max(tmp->key,ans);
tmp=tmp->rson;
}
else tmp=tmp->lson;
}
return ans;
} int suc(treap* &rt,int x)
{
int ans=INF;
treap *tmp=rt;
while (tmp)
{
if (tmp->key>x)
{
if (tmp->key==x && tmp->cnt>) return x;
ans=min(tmp->key,ans);//这里手滑写成了max(x,ans)orz
tmp=tmp->lson;
}
else tmp=tmp->rson;
}
return ans;
} int result()
{
int n,ans;
if(scanf("%d",&n) == EOF) n = ;
for (int i=;i<n;i++)
{
int turnover;
if(scanf("%d",&turnover) == EOF) turnover = ;
insert(root,turnover);
if (i==)
{
ans=turnover;
}
else
{
int premax,sucmin,ad=INF;
premax=pre(root,turnover);
sucmin=suc(root,turnover);
if (premax!=-INF && premax!=INF) ad=min(ad,turnover-premax);
if (sucmin!=INF && sucmin!=-INF) ad=min(ad,sucmin-turnover);
if (ad!=INF && ad!=-INF) ans=ans+ad;
}
}
return ans;
} int main()
{
printf("%d",result());
return ;
}

【Treap】BZOJ1588-[HNOI2002]营业额统计的更多相关文章

  1. bzoj1588 [HNOI2002]营业额统计(Treap)

    1588: [HNOI2002]营业额统计 Time Limit: 5 Sec  Memory Limit: 162 MBSubmit: 11485  Solved: 4062[Submit][Sta ...

  2. [BZOJ1588][HNOI2002]营业额统计 无旋Treap

    [HNOI2002]营业额统计 时间限制: 5 Sec  内存限制: 162 MB 题目描述 营业额统计 Tiger最近被公司升任为营业部经理,他上任后接受公司交给的第一项任务便是统计并分析公司成立以 ...

  3. BZOJ1588: [HNOI2002]营业额统计[BST]

    1588: [HNOI2002]营业额统计 Time Limit: 5 Sec  Memory Limit: 162 MBSubmit: 14151  Solved: 5366[Submit][Sta ...

  4. BZOJ1588 HNOI2002 营业额统计 [Splay入门题]

    [HNOI2002]营业额统计 Time Limit: 5 Sec  Memory Limit: 162 MBSubmit: 4128  Solved: 1305 Description 营业额统计 ...

  5. 【链表】BZOJ1588: [HNOI2002]营业额统计

    1588: [HNOI2002]营业额统计 Time Limit: 5 Sec  Memory Limit: 162 MBSubmit: 17555  Solved: 7179[Submit][Sta ...

  6. BZOJ1588 [HNOI2002]营业额统计 splay模板

    1588: [HNOI2002]营业额统计 Time Limit: 5 Sec  Memory Limit: 162 MB Submit: 16189  Solved: 6482 [Submit][S ...

  7. bzoj1588: [HNOI2002]营业额统计(权值线段树)

    1588: [HNOI2002]营业额统计 Time Limit: 5 Sec  Memory Limit: 162 MBSubmit: 16863  Solved: 6789[Submit][Sta ...

  8. bzoj1588: [HNOI2002]营业额统计(splay)

    1588: [HNOI2002]营业额统计 题目:传送门 题解: 复习splay所以来刷个水... 题目描述不是特别清楚:应该是找第i天以前一个最小的营业额和第i天做差的最小值作为第i天的最小波动值 ...

  9. [BZOJ1588] [HNOI2002] 营业额统计 (treap)

    Description Tiger最近被公司升任为营业部经理,他上任后接受公司交给的第一项任务便是统计并分析公司成立以来的营业情况. Tiger拿出了公司的账本,账本上记录了公司成立以来每天的营业额. ...

  10. bzoj1588 [HNOI2002]营业额统计 (treap)

    平衡树裸题 只需要求前驱后驱 treap写法 const mm=<<; maxnumber=; maxn=; var left,right,fix,key:..maxn]of longin ...

随机推荐

  1. ng 构建

    1.ng 构建和部署 构建:编译和合并ng build 部署:复制dist里面的文件到服务器 2.多环境的支持 配置环境package.json "scripts": { &quo ...

  2. Python爬虫学习笔记之爬取新浪微博

    import requests from urllib.parse import urlencode from pyquery import PyQuery as pq from pymongo im ...

  3. [bzoj2251][2010Beijing Wc]外星联络——后缀数组+暴力求解

    Brief Description 找到 01 串中所有重复出现次数大于 1 的子串.并按字典序输出他们的出现次数. Algorithm Design 求出后缀数组之后,枚举每一个后缀,对于每个后缀从 ...

  4. http://www.himigame.com/mac-cocoa-application/893.html

    [Cocoa(mac) Application 开发系列之一]创建第一个application—计算器 终于HTTP与Socket服务器以及cocos2dx之间的通信各种框架成功完成后,现在抽时间学习 ...

  5. android 调试 native 程序的方法

    一.背景 首先说需求,这个需求非常常见,就是android上需要的一个功能,linux已经有开源代码而且非常稳定,希望能直接porting过去使用,这个程序是pure c 的代码,也就是说,跟andr ...

  6. (接口自动化)Python3操作MySQL数据库

    基础语法: import pymysql #导入模块 conn = pymysql.connect(host='localhost',user='root', passwd='123456', db= ...

  7. pool.map的第二个参数想传入多个咋整?

    from functools import partial from multiprocessing import Pool as ThreadPool pageurls=[] if maxpage: ...

  8. centos 自启动

    https://blog.phpha.com/backup/archives/1458.html 1.服务 chkconfig 服务名 on 查看所有可以 chkconfig --list 2 修改 ...

  9. python变现实现新浪微博登陆

    新浪微博的登陆现在是越来越那个了,以前的模拟浏览器登陆新浪微博貌似也越来不管用了 登陆信息由以前的form变成了现在javascript,javascript的加载居然用了一个javascript的函 ...

  10. Django基础之视图

    Django的View(视图) 一个视图函数(类),简称视图,是一个简单的Python 函数(类),它接受Web请求并且返回Web响应. 响应可以是一张网页的HTML内容,一个重定向,一个404错误, ...