Splay入门题目 [HNOI2002]营业额统计

题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1588

这道题貌似很多中做法,我先是用multiset交了一发,然后又写了一发splay。

multiset做法,这个其实就是二分了,只是用set来保持加入一个元素时保持有序

 1 #include <set>
 2 #include <map>
 3 #include <cmath>
 4 #include <ctime>
 5 #include <queue>
 6 #include <stack>
 7 #include <cctype>
 8 #include <cstdio>
 9 #include <string>
10 #include <vector>
11 #include <cstdlib>
12 #include <cstring>
13 #include <iostream>
14 #include <algorithm>
15 using namespace std;
16 typedef unsigned long long ull;
17 typedef long long ll;
18 const int inf = 0x3f3f3f3f;
19 const double eps = 1e-8;
20 multiset<int>S;
21 multiset<int>::iterator it;
22 int main(void)
23 {
24     #ifndef ONLINE_JUDGE
25         freopen("in.txt","r",stdin);
26     #endif
27     int n;
28     while (~scanf ("%d",&n))
29     {
30         int ans = 0;
31         S.clear();
32         for (int i = 0; i < n; i++)
33         {
34             int x;
35             if (scanf ("%d",&x) == EOF)
36                 x = 0;
37             it = S.lower_bound(x);
38             if (it == S.begin())
39                 ans += abs(*it-x);
40             else
41             {
42                 int s1 = *it;
43                 int s2 = *--it;
44                 ans +=  min(abs(s1 - x),abs(s2 - x));
45             }
46             S.insert(x);
47         }
48         printf("%d\n",ans);
49     }
50     return 0;
51 }
View Code

splay

  1 #include <set>
  2 #include <map>
  3 #include <cmath>
  4 #include <ctime>
  5 #include <queue>
  6 #include <stack>
  7 #include <cctype>
  8 #include <cstdio>
  9 #include <string>
 10 #include <vector>
 11 #include <cstdlib>
 12 #include <cstring>
 13 #include <iostream>
 14 #include <algorithm>
 15 using namespace std;
 16 typedef unsigned long long ull;
 17 typedef long long ll;
 18 const int inf = 0x3f3f3f3f;
 19 const double eps = 1e-8;
 20 template <class T>
 21 inline bool scan_d(T &ret)
 22 {
 23     char c;
 24     int sgn;
 25     if(c=getchar(),c==EOF)
 26         return 0;
 27     while(c!='-'&&(c<'0'||c>'9'))
 28         c=getchar();
 29     sgn = (c=='-')?-1:1;
 30     ret =(c=='-')?0:(c-'0');
 31     while(c=getchar(),c>='0'&&c<='9')
 32         ret=ret*10+(c-'0');
 33     ret*=sgn;
 34     return 1;
 35 }
 36 const int maxn = 1e5+10;
 37 int fa[maxn],son[maxn][2],key[maxn],tot,root;
 38 
 39 
 40 
 41 void addNode(int &r,int father,int k)
 42 {
 43     r = ++tot;
 44     fa[r] = father;
 45     key[r] = k;
 46     son[r][0] = son[r][1] = 0;
 47 }
 48 
 49 void Rotate(int r,int kind)
 50 {
 51     int y = fa[r];
 52     son[y][!kind] = son[r][kind];
 53     fa[son[r][kind]] = y;
 54     if (fa[y])
 55         son[fa[y]][son[fa[y]][1]==y] = r;
 56     son[r][kind] = y;
 57     fa[r] = fa[y];
 58     fa[y] = r;
 59 }
 60 void splay(int r,int goal)
 61 {
 62     while (fa[r] != goal)
 63     {
 64         if (fa[fa[r]] == goal)
 65         {
 66             Rotate(r,son[fa[r]][0] == r);
 67         }
 68         else
 69         {
 70             int y = fa[r];
 71             int kind = (son[fa[y]][0] == y);
 72             if (son[y][kind] == r)
 73             {
 74                 Rotate(r,!kind);
 75                 Rotate(r,kind);
 76             }
 77             else
 78             {
 79                 Rotate(y,kind);
 80                 Rotate(r,kind);
 81             }
 82         }
 83     }
 84     if (goal == 0)
 85         root = r;
 86 }
 87 bool Insert(int k)
 88 {
 89     int r = root;
 90     while (son[r][k > key[r]])
 91     {
 92         if (k == key[r])
 93         {
 94             splay(r,0);
 95             return false;
 96         }
 97         r = son[r][k>key[r]];
 98     }
 99     addNode(son[r][k>key[r]],r,k);
100     splay(son[r][k>key[r]],0);
101     return true;
102 }
103 int get_pre(int r)
104 {
105     int tmp = son[r][0];
106     if (tmp == 0)
107         return inf;
108     while (son[tmp][1])
109         tmp = son[tmp][1];
110     return  key[tmp];
111 }
112 int get_next(int r)
113 {
114     int tmp = son[r][1];
115     if (tmp==0)
116         return inf;
117     while (son[tmp][0])
118         tmp = son[tmp][0];
119     return key[tmp] ;
120 }
121 int main(void)
122 {
123     #ifndef ONLINE_JUDGE
124         freopen("in.txt","r",stdin);
125     #endif
126     int n;
127     while (~scanf ("%d",&n))
128     {
129         tot = 0;
130         root = 0;
131         int ans = 0;
132         for (int i = 0; i < n; i++)
133         {
134             int x;
135             if (scanf ("%d",&x) == EOF)
136                 x = 0;
137             if (i == 0)
138             {
139                 ans += x;
140                 addNode(root,0,x);
141             }
142             else
143             {
144                 if (!Insert(x))
145                     continue;
146                 int l = get_pre(root);
147                 int r = get_next(root);
148                 ans += min(abs(x-l),abs(x-r));
149                 //ans += min(l,r);
150             }
151         }
152         printf("%d\n",ans);
153     }
154     return 0;
155 }
View Code

 

posted @ 2014-10-26 20:51  PlasticSpirit  阅读(228)  评论(0编辑  收藏  举报