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( ...
随机推荐
- Dockerfile创建自定义Docker镜像以及CMD与ENTRYPOINT指令的比较
1.概述 创建Docker镜像的方式有三种 docker commit命令:由容器生成镜像: Dockerfile文件+docker build命令: 从本地文件系统导入:OpenVZ的模板. 关于这 ...
- 同态加密-Homomorphic encryption
同态加密(Homomorphic encryption)是一种加密形式,它允许人们对密文进行特定的代数运算得到仍然是加密的结果,将其解密所得到的结果与对明文进行同样的运算结果一样.换言之,这项技术令人 ...
- C++成员变量的初始化顺序问题
问题来源: 由于面试题中,考官出了一道简单的程序输出结果值的题:如下, class A { private: int n1; int n2; public: A():n2(0),n1(n2+2){} ...
- MyBatis学习--SqlMapConfig.xml配置文件
简介 SqlMapConfig.xml是MyBatis的全局配置文件,在前面的文章中我们可以看出,在SqlMapConfig.xml主要是配置了数据源.事务和映射文件,其实在SqlMapConfig. ...
- 标准Web系统的架构分层
标准Web系统的架构分层 – 转载请注明出处 1.架构体系分层图 在上图中我们描述了Web系统架构中的组成部分.并且给出了每一层常用的技术组件/服务实现.需要注意以下几点: 系统架构是灵活的,根据需求 ...
- iOS开发中的错误整理,AFN框架和MJRefresh框架搭配应该注意的问题
注意问题一:每次请求之前先将之前的请求取消 注意问题二:请求成功之后要判断footer或者header的显示状态 首次下拉刷新,要判断是否已经全部 ...
- 【URAL 1297】Palindrome 最长回文子串
模板题,,,模板打错查了1h+QAQ #include<cmath> #include<cstdio> #include<cstring> #include< ...
- 网络流HDU 2883
建图 源点 -> 每个人 -> 每段时间 -> 汇点 时间要离散化一下 分成一些时间段 权 ...
- 51nod 1441 欧拉筛法
1441 士兵的数字游戏 题目来源: CodeForces 基准时间限制:6 秒 空间限制:131072 KB 分值: 40 难度:4级算法题 收藏 关注 两个士兵正在玩一个游戏,游戏开始的时候, ...
- RFID标签
定义: RFID无线射频识别是一种非接触式的自动识别技术,它通过射频信号自动识别目标对象并获取相关数据,识别工作无须人工干预,可工作于各种恶劣环境.RFID技术可识别高速运动物体并可同时识别多个电子标 ...