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( ...
随机推荐
- Bootstrap系列 -- 40. 导航条二级菜单
在导航条中添加二级菜单也非常简单 <div class="navbar navbar-default" role="navigation"> < ...
- 将IList转换为List
简单点说,IList<T>直接转换为List<T>可以不用考虑.IList<T>可以用至少2种方式简单的复制成List<T>:1.IList<T ...
- 《TCP/IP详解卷1:协议》第1章 概述-读书笔记
章节回顾: <TCP/IP详解卷1:协议>第1章 概述-读书笔记 <TCP/IP详解卷1:协议>第2章 链路层-读书笔记 <TCP/IP详解卷1:协议>第3章 IP ...
- Mysql与Redis的同步实践
一.测试环境在Ubuntu kylin 14.04 64bit 已经安装Mysql.Redis.php.lib_mysqludf_json.so.Gearman. 点击这里查看测试数据库及表参考 本文 ...
- 【心得】怪异的JS的Date函数
我们知道new Date('2013/1/1')是2013年1月1日, 那么new Date('2013/1/366')会报无效日期格式吗?答案是,这是一个有效的日期,但是他是表示2014年1月1日. ...
- MyBatis学习--mybatis开发dao的方法
简介 使用Mybatis开发Dao,通常有两个方法,即原始Dao开发方法和Mapper接口开发方法. 主要概念介绍: MyBatis中进行Dao开发时候有几个重要的类,它们是SqlSessionFac ...
- android textview改变部分文字的颜色和string.xml中文字的替换(转)
转 :http://blog.csdn.net/ljz2009y/article/details/23878669 一:TextView组件改变部分文字的颜色: TextView textView ...
- Dubbo系列(1)_背景介绍和基本情况
一.本文目的 主要介绍Dubbo的产生背景和需要解决的问题 二.产生背景 随着大数据量.高并发的互联网应用越来越多,单机系统已经无法满足系统的需要.通过SOA搭建一个分 ...
- 简单的JS控制button颜色随点击更改
先上效果图: 默认“今日”是选中状态,是行内样式: <button type="button" id="today" class="btn-li ...
- Win7另存文件没有桌面的解决方法
今日一朋友保存文件随口说了一句我那个桌面找不到了...我略感惊奇,没遇到这么个情况.决定,问度娘,在此做下记录,以便以后自己或朋友查阅. 我们在网上另存一个文件,打开另存窗口,发现没有“桌面” 在收藏 ...