-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathHYSBZ-1588-HNOI2002营业额统计.cpp
136 lines (131 loc) · 2.79 KB
/
HYSBZ-1588-HNOI2002营业额统计.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
* Algorithm: Splay
* Date: 2015-08-26
* Contest: 练习
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<string>
#include<queue>
#include<deque>
#include<stack>
#include<map>
#include<set>
#define INF 1<<29
#define mod 1000000007
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
typedef long long LL;
const int N=100007;
struct SplayTree
{
int son[N][2],pre[N],val[N];
int rt,sz;
inline void Rotate(int x,int c)//旋转
{
int y=pre[x];
son[y][!c]=son[x][c];
pre[son[x][c]]=y;
pre[x]=pre[y];
if(pre[y])
son[pre[y]][son[pre[y]][1]==y]=x;
son[x][c]=y;
pre[y]=x;
}
inline void Splay(int x,int goal)
{
while(pre[x]!=goal)
{
if(pre[pre[x]]==goal)
{
if(son[pre[x]][0]==x) Rotate(x,1);
else Rotate(x,0);
}
else
{
int y=pre[x],z=pre[y];
if(son[z][0]==y)
{
if(son[y][0]==x)
Rotate(x,1),Rotate(x,1);
else
Rotate(x,0),Rotate(x,1);
}
else
{
if(son[y][0]==x)
Rotate(x,1),Rotate(x,0);
else
Rotate(x,0),Rotate(x,0);
}
}
}
if(goal==0) rt=x;
}
inline void NewNode(int y,int &x,int a)
{
x=++sz;
pre[x]=y;
val[x]=a;
son[x][0]=son[x][1]=0;
}
inline void init()
{
sz=0;
NewNode(0,rt,-INF);
NewNode(rt,son[rt][1],INF);
}
inline void Insert(int a)
{
int x=rt;
while(son[x][val[x]<a]) x=son[x][val[x]<a];
NewNode(x,son[x][val[x]<a],a);
Splay(sz,0);
}
inline int fx_min(int a)
{
int x=rt,minn=INF;
while(x)
{
if(val[x]==a) return a;
if(val[x]>a) minn=min(minn,val[x]),x=son[x][0];
else x=son[x][1];
}
return minn;
}
inline int fx_max(int a)
{
int x=rt,maxx=-INF;
while(x)
{
if(val[x]==a) return a;
if(val[x]<a) maxx=max(maxx,val[x]),x=son[x][1];
else
x=son[x][0];
}
return maxx;
}
}spt;
int main()
{
int n;
scanf("%d",&n);
int v,ans;
scanf("%d",&v);
ans=v;
spt.init();
spt.Insert(v);
for(int i=1;i<n;i++)
{
if(scanf("%d",&v)==-1) v=0;
//scanf("%d",&v);
ans+=min(fabs(v-spt.fx_min(v)),fabs(spt.fx_max(v)-v));
spt.Insert(v);
}
cout<<ans<<endl;
return 0;
}