TYVJ1185 营业额统计

也是旧题重做,本次是splay,原来写的treap版本请移步:http://www.cnblogs.com/evan-oi/archive/2012/02/03/2337324.html

 

也是splay水题不多说了。

 

View Code
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
using namespace std;
#define MaxN 10010
#define INF 2000000
int n,tot=0,root=0,ans=0;
struct atp
{
int data,f,l,r,cnt;
}a[MaxN*8];

void Right(int x)
{
int y=a[x].f;
int z=a[y].f,c=a[x].r;
if (a[z].l==y) a[z].l=x; else a[z].r=x; a[x].f=z;
a[x].r=y;a[y].f=x;
a[y].l=c;a[c].f=y;
}
void Left(int x)
{
int y=a[x].f;
int z=a[y].f,c=a[x].l;
if (a[z].l==y) a[z].l=x; else a[z].r=x; a[x].f=z;
a[x].l=y;a[y].f=x;
a[y].r=c;a[c].f=y;
}
void splay(int &root,int x)
{
int y,z;
while (x!=root)
{
y=a[x].f;z=a[y].f;
if (y==root)
{
if (a[y].l==x) Right(x); else Left(x);
root=x;
} else
if (a[z].l==y)
if (a[y].l==x) Right(y),Right(x); else
Left(x),Right(x); else
if (a[y].r==x) Left(y),Left(x); else
Right(x),Left(x);
if (z==root) root=x;
}
}
void insert(int &t,int x,int f)
{
if (t==0)
{
tot++;
t=tot;
a[t].l=a[t].r=0;
a[t].f=f;
a[t].data=x;
a[t].cnt=1;
splay(root,t);
} else
if (x<a[t].data) insert(a[t].l,x,t); else
if(x>a[t].data) insert(a[t].r,x,t); else
if (x==a[t].data) a[t].cnt++,splay(root,t);
}
int Max(int t)
{
if (t==0) return INF;
if (a[t].r!=0) return Max(a[t].r); else
return a[t].data;
}
int Min(int t)
{
if (t==0) return INF;
if (a[t].l!=0) return Min(a[t].l); else
return a[t].data;
}
int main()
{
int x,t1,t2;
freopen("in","r",stdin);
//freopen("1.out","w",stdout);
scanf("%d",&n);
scanf("%d",&x);
ans=x;insert(root,x,0);
for (int i=2;i<=n;i++)
{
scanf("%d",&x);
insert(root,x,0);
if (a[root].cnt==1)
{
t1=Max(a[root].l);
t2=Min(a[root].r);
ans+=min(abs(x-t1),abs(x-t2));
}
}
printf("%d",ans);
return 0;
}



转载于:https://www.cnblogs.com/evan-oi/archive/2012/03/31/2427331.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值