BZOJ 1588: [HNOI2002]营业额统计 双向链表 / splay / treap
1588: [HNOI2002]营业额统计
Description
营业额统计 Tiger最近被公司升任为营业部经理,他上任后接受公司交给的第一项任务便是统计并分析公司成立以来的营业情况。 Tiger拿出了公司的账本,账本上记录了公司成立以来每天的营业额。分析营业情况是一项相当复杂的工作。由于节假日,大减价或者是其他情况的时候,营业额会出现一定的波动,当然一定的波动是能够接受的,但是在某些时候营业额突变得很高或是很低,这就证明公司此时的经营状况出现了问题。经济管理学上定义了一种最小波动值来衡量这种情况: 该天的最小波动值 当最小波动值越大时,就说明营业情况越不稳定。 而分析整个公司的从成立到现在营业情况是否稳定,只需要把每一天的最小波动值加起来就可以了。你的任务就是编写一个程序帮助Tiger来计算这一个值。 第一天的最小波动值为第一天的营业额。 输入输出要求
Input
第一行为正整数 ,表示该公司从成立一直到现在的天数,接下来的n行每行有一个整数(有可能有负数) ,表示第i天公司的营业额。
Output
输出文件仅有一个正整数,即Sigma(每天最小的波动值) 。结果小于2^31 。
Sample Input
5
1
2
5
4
6
Sample Output
HINT
结果说明:5+|1-5|+|2-1|+|5-5|+|4-5|+|6-5|=5+4+1+0+1+1=12
该题数据bug已修复.----2016.5.15
题解:
双向链表:http://www.open-open.com/doc/view/a46375420b6a43f08e0862489069a11d
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
#pragma comment(linker, "/STACK:102400000,102400000")
#define ls i<<1
#define rs ls | 1
#define mid ((ll+rr)>>1)
typedef long long LL;
const LL INF = (1LL<<)-;
const double Pi = acos(-1.0);
const int N = 1e5+, M = 1e6+, mod = 1e9+, inf = (<<)-; struct ss {int x,id;
bool operator < (const ss &r) const
{
return x<r.x;
}
}a[N];
int ans,n,Rank[N],lef[N],righ[N];
int main() {
scanf("%d",&n);
for(int i = ; i <= n; ++i) scanf("%d",&a[i].x),a[i].id=i;
ans = a[].x;
sort(a+,a+n+);
for(int i = ; i <= n; ++i) Rank[a[i].id] = i;
for(int i = ; i <= n; ++i) lef[i] = i-, righ[i] = i+;
righ[n] = ;
for(int i = n; i >=; --i) {
int x = Rank[i];
if(lef[x] && righ[x]) {
ans += min(a[x].x - a[lef[x]].x,a[righ[x]].x - a[x].x);
righ[lef[x]] = righ[x];
lef[righ[x]] = lef[x];
} else if(!lef[x] && righ[x]) {
ans += a[righ[x]].x - a[x].x;
lef[righ[x]] = ;
} else if(!righ[x] && lef[x]){
ans += a[x].x - a[lef[x]].x;
righ[lef[x]] = ;
}
}
printf("%d\n",ans);
}
双向链表
treap写法
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
#pragma comment(linker, "/STACK:102400000,102400000")
#define ls i<<1
#define rs ls | 1
#define mid ((ll+rr)>>1)
typedef long long LL;
const LL INF = (1LL<<)-;
const double Pi = acos(-1.0);
const int N = 1e5+, M = 1e6+, mod = 1e9+, inf = (<<)-; struct data {int l,r,v,size,rnd,w;}tr[N * ];
int n,size=,root,ans,Ans;
void update(int k) {tr[k].size=tr[tr[k].l].size+tr[tr[k].r].size+tr[k].w;}
void rturn (int &k){
int t=tr[k].l;tr[k].l=tr[t].r;tr[t].r=k;
tr[t].size=tr[k].size;update(k);k=t;
}
void lturn(int &k) {
int t=tr[k].r;tr[k].r=tr[t].l;tr[t].l=k;
tr[t].size=tr[k].size;update(k);k=t;
}
void insert(int &k,int x) {
if(k == ) {
size++;k=size;
tr[k].size=tr[k].w=;
tr[k].v=x;
tr[k].rnd=rand();
return ;
}
tr[k].size++;
if(tr[k].v == x) tr[k].w++;
else if(x > tr[k].v) {
insert(tr[k].r,x);
if(tr[tr[k].r].rnd<tr[k].rnd) lturn(k);
} else {
insert(tr[k].l,x);
if(tr[tr[k].l].rnd<tr[k].rnd) rturn(k);
}
}
void query_pre(int k,int x) {
if(k == ) return ;
if(tr[k].v <= x) {
ans=tr[k].v;query_pre(tr[k].r,x);
} else query_pre(tr[k].l,x);
}
void query_nex(int k,int x) {
if(k == ) return ;
if(tr[k].v >= x) {
ans=tr[k].v;;query_nex(tr[k].l,x);
} else query_nex(tr[k].r,x);
} int main() {
root = ;
scanf("%d",&n);
for(int i = ; i <= n; ++i) {
int x;
scanf("%d",&x);
if(i == ) {
Ans = x;
} else {
ans = -inf;query_pre(root,x);
int fi = ans;
ans = inf;query_nex(root,x);
int se = ans;
//cout<<fi<<" "<<se<<endl;
if(fi!=-inf || se != inf) Ans += min(x-fi,se-x);
}
insert(root,x);
}
printf("%d\n",Ans);
return ;
}
treap
splay(HZWER)
#include<iostream>
#include<cstdio>
#define inf 1000000000
using namespace std;
int ans,n,t1,t2,rt,size;
int tr[][],fa[],num[];
void rotate(int x,int &k)
{
int y=fa[x],z=fa[y],l,r;
if(tr[y][]==x)l=;else l=;r=l^;
if(y==k)k=x;
else{if(tr[z][]==y)tr[z][]=x;else tr[z][]=x;}
fa[x]=z;fa[y]=x;fa[tr[x][r]]=y;
tr[y][l]=tr[x][r];tr[x][r]=y;
}
void splay(int x,int &k)
{
int y,z;
while(x!=k)
{
y=fa[x],z=fa[y];
if(y!=k)
{
if((tr[y][]==x)^(tr[z][]==y))rotate(x,k);
else rotate(y,k);
}
rotate(x,k);
}
}
void ins(int &k,int x,int last)
{
if(k==){size++;k=size;num[k]=x;fa[k]=last;splay(k,rt);return;}
if(x<num[k])ins(tr[k][],x,k);
else ins(tr[k][],x,k);
}
void ask_before(int k,int x)
{
if(k==)return;
if(num[k]<=x){t1=num[k];ask_before(tr[k][],x);}
else ask_before(tr[k][],x);
}
void ask_after(int k,int x)
{
if(k==)return;
if(num[k]>=x){t2=num[k];ask_after(tr[k][],x);}
else ask_after(tr[k][],x);
}
int main()
{
scanf("%d",&n);
for(int i=;i<=n;i++)
{
int x;if(scanf("%d",&x)==EOF)x=;
t1=-inf;t2=inf;
ask_before(rt,x);
ask_after(rt,x);
if(i!=)ans+=min(x-t1,t2-x);
else ans+=x;
ins(rt,x,);
}
printf("%d",ans);
return ;
}
splay
BZOJ 1588: [HNOI2002]营业额统计 双向链表 / splay / treap的更多相关文章
- BZOJ 1588: [HNOI2002]营业额统计 双向链表
BZOJ 1588: [HNOI2002]营业额统计 Time Limit: 5 Sec Memory Limit: 512 MBSubmit: 9619 Solved: 3287 题目连接 ht ...
- bzoj 1588: [HNOI2002]营业额统计(splay入门)
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1588 题解:这题如果用普通的bst的话是可以过时间差不多4s左右如果用splay的话是14 ...
- bzoj 1588: [HNOI2002]营业额统计 treap
1588: [HNOI2002]营业额统计 Time Limit: 5 Sec Memory Limit: 162 MBSubmit: 13902 Solved: 5225[Submit][Sta ...
- 2018.07.06 BZOJ 1588: HNOI2002营业额统计(非旋treap)
1588: [HNOI2002]营业额统计 Time Limit: 5 Sec Memory Limit: 162 MB Description 营业额统计 Tiger最近被公司升任为营业部经理,他上 ...
- Bzoj 1588: [HNOI2002]营业额统计(splay)
1588: [HNOI2002]营业额统计 Time Limit: 5 Sec Memory Limit: 162 MB Description 营业额统计 Tiger最近被公司升任为营业部经理,他上 ...
- BZOJ 1588: [HNOI2002]营业额统计
1588: [HNOI2002]营业额统计 Time Limit: 5 Sec Memory Limit: 162 MBSubmit: 14396 Solved: 5521[Submit][Sta ...
- 数据结构:(平衡树,链表)BZOJ 1588[HNOI2002]营业额统计
1588: [HNOI2002]营业额统计 Time Limit: 5 Sec Memory Limit: 162 MBSubmit: 12173 Solved: 4354[Submit][Sta ...
- SET || BZOJ 1588: [HNOI2002]营业额统计 || Luogu P2234 [HNOI2002]营业额统计
题面:P2234 [HNOI2002]营业额统计 题解:随便写写 注意:cmath中abs函数返回的是一个浮点数,在bzoj上会ce 代码: #include<cstdio> #inclu ...
- bzoj 1588 [HNOI2002] 营业额统计 链表和Splay
来自HNOI 2002营业额的统计一题,这题以前是用链表水过的,最近看见许多splay的题,赶紧张一下知识. 题目大意就是对于一个序列,输出每个元素与它之前元素的差的最小值的和.先说链表的方法吧. 大 ...
随机推荐
- JS添加删除DIV
function addDiv(w,h){ //如果原来有“divCell”这个图层,先删除这个图层 deleteDiv(); //创建一个div var my = d ...
- C#之枚举类型
参考: http://www.cnblogs.com/an-wl/archive/2011/04/14/2015815.html 惯例先上MSDN: https://msdn.microsoft.co ...
- Unity3d 制作物品平滑运动
直接贴代码了 using UnityEngine; using System; using System.Collections; using System; using DataTable; pub ...
- poj 1102.LC-Display 解题报告
题目链接:http://poj.org/problem?id=1102 题目意思:就是根据给出的格式 s 和 数字 n,输出数值 n 的 LCD 显示.数值 n 的每个数字要占据 s + 2 列 和 ...
- 如何让两个 并列的div高度相等
哪个div Height值大,就将其值赋给Height值小的div,从而使2个div高度始终保持一致. function $(id){ return document.getElementById(i ...
- 掌握VS2010调试 -- 入门指南
1 导言 在软件开发周期中,测试和修正缺陷(defect,defect与bug的区别:Bug是缺陷的一种表现形式,而一个缺陷是可以引起多种Bug的)的时间远多于写代码的时间.通常,debug是指发现缺 ...
- .NET微信公众号开发-2.0创建自定义菜单
一.前言 开发之前,我们需要阅读官方的接口说明文档,不得不吐槽一下,微信的这个官方文档真的很烂,但是,为了开发我们需要的功能,我们也不得不去看这些文档. 接口文档地址:http://mp.weixin ...
- [Android Pro] Toolbar的完全自定义
reference to : http://blog.csdn.net/elder_sword/article/details/46634751 Toolbar是什么,不知道的可以去大神的博客瞻仰下 ...
- javascript事件与event对象的属性
javascript事件列表解说 事件 浏览器支持 解说 一般事件 onclick IE3.N2 鼠标点击时触发此事件 ondblclick IE4.N4 鼠标双击时触发此事件 onmousedown ...
- 同一内网不能网段ping 不通
[root@NB sysconfig]# route Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use ...