BZOJ1588[HNOI2002]营业额统计
平衡树常规题,给出两种实现算法
Treap版:
//OJ 1610
//by Cydiater
//2016.9.1
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <string>
#include <queue>
#include <map>
#include <algorithm>
#include <cmath>
#include <ctime>
using namespace std;
#define ll long long
#define up(i,j,n) for(int i=j;i<=n;i++)
#define down(i,j,n) for(int i=j;i>=n;i--)
;
const int oo=0x3f3f3f3f;
inline int read(){
,f=;
;ch=getchar();}
+ch-';ch=getchar();}
return x*f;
}
,tol=,maxx,minn,delta;
ll ans;
struct node{
int leftt,rightt,rnd,v;
}t[MAXN];
namespace solution{
void lefturn(int &k){
int tt=t[k].rightt;t[k].rightt=t[tt].leftt;t[tt].leftt=k;k=tt;
}
void righturn(int &k){
int tt=t[k].leftt;t[k].leftt=t[tt].rightt;t[tt].rightt=k;k=tt;
}
void insert(int &k,int x){
){
k=++tol;
t[k].leftt=t[k].rightt=;
t[k].v=x;t[k].rnd=rand();
return;
}
if(x==t[k].v)return;
else if(x>t[k].v){
insert(t[k].rightt,x);
if(t[t[k].rightt].rnd<t[k].rnd)lefturn(k);
}
else if(x<t[k].v){
insert(t[k].leftt,x);
if(t[t[k].leftt].rnd<t[k].rnd)righturn(k);
}
}
void query_delta(int k,int x){
) return;
if(x<=t[k].v)maxx=min(maxx,t[k].v);
if(x>=t[k].v)minn=max(minn,t[k].v);
if(x>t[k].v)query_delta(t[k].rightt,x);
else query_delta(t[k].leftt,x);
}
void slove(){
N=read();
up(i,,N){
num=read();
maxx=oo;minn=-oo;delta=oo;
query_delta(root,num);
if(maxx!=-oo)delta=min(delta,abs(maxx-num));
if(minn!=oo)delta=min(delta,abs(num-minn));
)delta=num;
ans+=delta;
//cout<<num<<' '<<delta<<' '<<ans<<endl;
insert(root,num);
}
}
void output(){
cout<<ans<<endl;
}
}
int main(){
//freopen("input.in","r",stdin);
//freopen("output.out","w",stdout);
using namespace solution;
slove();
output();
;
}
Splay版:
//bzoj 1588
//by Cydiater
//2016.9.6
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <algorithm>
#include <queue>
#include <map>
#include <ctime>
#include <cmath>
#include <iomanip>
using namespace std;
#define ll long long
#define up(i,j,n) for(int i=j;i<=n;i++)
#define down(i,j,n) for(int i=j;i>=n;i--)
;
const int oo=0x3f3f3f3f;
inline int read(){
,f=;
;ch=getchar();}
+ch-';ch=getchar();}
return x*f;
}
map<int,int>lable;
struct SplayTree{
],v,siz,cnt,fa;
}t[MAXN];
,tol=,anss=;
namespace solution{
]==x;}
]=t[x].son[]=t[x].siz=t[x].cnt=t[x].fa=t[x].v=;}
void updata(int x){
if(x){
t[x].siz=t[x].cnt;
])t[x].siz+=t[t[x].son[]].siz;
])t[x].siz+=t[t[x].son[]].siz;
}
}
inline void rotate(int x){//rotate now node to root
int old=t[x].fa,oldf=t[old].fa,which=get(x);
t[old].son[which]=t[x].son[which^];t[t[old].son[which]].fa=old;
t[old].fa=x;t[x].son[which^]=old;
t[x].fa=oldf;
]==old]=x;
updata(old);updata(x);
}
void splay(int x){
for(int fa;(fa=t[x].fa);rotate(x))if(t[fa].fa)
rotate(get(x)==get(fa)?fa:x);root=x;
}
inline void insert(int v){
){
root=++tol;
t[root].son[]=t[root].son[]=t[root].fa=;
t[root].v=v;t[root].cnt=t[root].siz=;
return;
}
;
){
if(t[now].v==v){
t[now].cnt++;updata(now);updata(fa);
splay(now);break;
}
fa=now;now=t[now].son[t[now].v<v];
){
now=++tol;
t[now].son[]=t[now].son[]=;t[now].v=v;
t[now].siz=t[now].cnt=;t[now].fa=fa;
t[fa].son[t[fa].v<v]=tol;
updata(fa);
splay(now);
break;
}
}
}
int find(int x){
;
){
];
else{
ans+=(t[now].son[]?t[t[now].son[]].siz:);
if(t[now].v==x){
splay(now);
;
}
ans+=t[now].cnt;
now=t[now].son[];
}
}
}
int pre(){
];
])now=t[now].son[];
return now;
}
int nxt(){
];
])now=t[now].son[];
return now;
}
void del(int x){
int whatever=find(x);
){
t[root].cnt--;
t[root].siz--;
}
]+t[root].son[]==){
clear(root);root=;
return;
}
]){
];t[root].fa=;
clear(oldroot);return;
}]){
];t[root].fa=;
clear(oldroot);return;
}
int leftbig=pre(),oldroot=root;splay(leftbig);
t[t[oldroot].son[]].fa=root;t[root].son[]=t[oldroot].son[];
clear(root);updata(root);
}
void debug(int now){
printf(],t[now].son[],t[now].siz,t[now].cnt,t[now].fa,t[now].v);
])debug(t[now].son[]);
])debug(t[now].son[]);
}
}
int main(){
//freopen("input.in","r",stdin);
//freopen("output.out","w",stdout);
using namespace solution;
N=read();
while(N--){
int num=read();if(lable[num])continue;
insert(num);lable[num]=;
int maxx=nxt(),minn=pre();
int tmp=oo;
)tmp=num;
if(maxx)tmp=min(tmp,t[maxx].v-num);
if(minn)tmp=min(tmp,num-t[minn].v);
anss+=tmp;
//debug(root);puts("");
}
printf("%d\n",anss);
;
}
BZOJ1588[HNOI2002]营业额统计的更多相关文章
- BZOJ1588: [HNOI2002]营业额统计[BST]
1588: [HNOI2002]营业额统计 Time Limit: 5 Sec Memory Limit: 162 MBSubmit: 14151 Solved: 5366[Submit][Sta ...
- BZOJ1588 HNOI2002 营业额统计 [Splay入门题]
[HNOI2002]营业额统计 Time Limit: 5 Sec Memory Limit: 162 MBSubmit: 4128 Solved: 1305 Description 营业额统计 ...
- bzoj1588 [HNOI2002]营业额统计(Treap)
1588: [HNOI2002]营业额统计 Time Limit: 5 Sec Memory Limit: 162 MBSubmit: 11485 Solved: 4062[Submit][Sta ...
- 【链表】BZOJ1588: [HNOI2002]营业额统计
1588: [HNOI2002]营业额统计 Time Limit: 5 Sec Memory Limit: 162 MBSubmit: 17555 Solved: 7179[Submit][Sta ...
- [BZOJ1588][HNOI2002]营业额统计 无旋Treap
[HNOI2002]营业额统计 时间限制: 5 Sec 内存限制: 162 MB 题目描述 营业额统计 Tiger最近被公司升任为营业部经理,他上任后接受公司交给的第一项任务便是统计并分析公司成立以 ...
- BZOJ1588 [HNOI2002]营业额统计 splay模板
1588: [HNOI2002]营业额统计 Time Limit: 5 Sec Memory Limit: 162 MB Submit: 16189 Solved: 6482 [Submit][S ...
- bzoj1588: [HNOI2002]营业额统计(权值线段树)
1588: [HNOI2002]营业额统计 Time Limit: 5 Sec Memory Limit: 162 MBSubmit: 16863 Solved: 6789[Submit][Sta ...
- bzoj1588: [HNOI2002]营业额统计(splay)
1588: [HNOI2002]营业额统计 题目:传送门 题解: 复习splay所以来刷个水... 题目描述不是特别清楚:应该是找第i天以前一个最小的营业额和第i天做差的最小值作为第i天的最小波动值 ...
- [BZOJ1588] [HNOI2002] 营业额统计 (treap)
Description Tiger最近被公司升任为营业部经理,他上任后接受公司交给的第一项任务便是统计并分析公司成立以来的营业情况. Tiger拿出了公司的账本,账本上记录了公司成立以来每天的营业额. ...
- BZOJ1588 [HNOI2002]营业额统计 set
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ1588 题意概括 给出数列,求 ∑F[i],其中F[1] = a[1] , F[i] = min( ...
随机推荐
- Nodejs进阶:核心模块https 之 如何优雅的访问12306
本文摘录自<Nodejs学习笔记>,更多章节及更新,请访问 github主页地址.欢迎加群交流,群号 197339705. 模块概览 这个模块的重要性,基本不用强调了.在网络安全问题日益严 ...
- win8/8.1 免密码登录设置
http://www.ehow.com/how_8013338_start-windows-password.html 原文见上方链接 1,win+r调出命令输入窗口,输入 netplwiz 回车 2 ...
- Eclipse自动补全功能管理
#这种方法只适用于Eclipse Classic版本(这个版本带有插件的源码) 在使用Eclispe的过程,感觉自动补全做的不好,没有VS的强大.下面说两个增强自动补全的方法: 1.增加Eclipse ...
- 东大OJ-快速排序
1236: Simple Sort 时间限制: 1 Sec 内存限制: 128 MB 提交: 195 解决: 53 [提交][状态][讨论版] 题目描述 You are given n ...
- shell读取文件内容
Shell脚本,执行解释速度快.代码简单易于理解.在shell代码编写过程中,经常会用到读取文件内容. 写法一: ------------------------------------ ...
- mysql性能优化-慢查询分析、优化索引和配置
一.优化概述 二.查询与索引优化分析 1性能瓶颈定位 Show命令 慢查询日志 explain分析查询 profiling分析查询 2索引及查询优化 三.配置优化 1) max_connec ...
- python基础-编码_if条件判断
一.第一句Python代码 在 /home/dev/ 目录下创建 hello.py 文件,内容如下: [root@python-3 scripts]# cat hello.py #!/usr/bin/ ...
- CSS Hack技术介绍及常用的Hack技巧
一.什么是CSS Hack? 不同的浏览器对CSS的解析结果是不同的,因此会导致相同的CSS输出的页面效果不同,这就需要CSS Hack来解决浏览器局部的兼容性问题.而这个针对不同的浏览器写不同的CS ...
- java中关于try、catch、finally中的细节分析
来源于:http://www.cnblogs.com/aigongsi/archive/2012/04/19/2457735.html 对所有的例子进行总结 1 try.catch.finally语句 ...
- zabbix 微信报警
http://blog.csdn.net/wh211212/article/details/52735236 Zabbix可以通过多种方式把告警信息发送到指定人,常用的有邮件,短信报警方式,但是越来越 ...