题意(引用):题意:有很多种草,有两个属性:价格和新鲜度;有很多牛,它们都会各自需求一种草,要求是其价格最低为ai,新鲜度最低为bi,且这些牛不希望自己的草和别人的一样。问要满足所有需求的最小花费是多少?

一开始想的都是各种匹配,结果正解是贪心……

应该来说想不到好方法时,不是二分答案就是贪心了吧?

先按新鲜度为第一关键字,价格为第二关键字排序

从最挑剔(新鲜度要求越高)的牛开始考虑,每次选择应当是价格最小并且能能满足当前牛的牧草

可以这样想:当前处理的牛使没选过的牛中最挑剔的,新鲜度能满足当前牛的一定也能满足之前牛

再来考虑价格,因为价格是最低要求,这次价格选择最低限度的,显然会为之后的选择打开更多空间

所以每次每次选择应当是价格最小并且能能满足当前牛的牧草是最优的

朴素模拟复杂度O(nm) ,所以需要各种数据结构优化

考虑到好久没有写平衡树(其实是一开始想到的就是平衡树)

于是就写了splay

  1 var cv,cw,v,w,d,fa:array[0..200010] of longint;
  2     son:array[0..200010,1..2] of longint;
  3     root,i,j,p,n,m,t,h:longint;
  4     ans:int64;
  5 function succ(x:longint):longint;   //找后继
  6   var p:longint;
  7   begin
  8     p:=son[x,2];
  9     while son[p,1]<>0 do  p:=son[p,1];
 10     exit(p);
 11   end;
 12 
 13 procedure rotate(x,w:longint);
 14   var y:longint;
 15   begin
 16     y:=fa[x];
 17     if fa[y]<>0 then
 18     begin
 19       if son[fa[y],1]=y then son[fa[y],1]:=x
 20       else son[fa[y],2]:=x;
 21     end;
 22     fa[x]:=fa[y];
 23     son[y,3-w]:=son[x,w];
 24     if son[x,w]<>0 then fa[son[x,w]]:=y;
 25     son[x,w]:=y;
 26     fa[y]:=x;
 27   end;
 28 
 29 procedure splay(x:longint);     //拍习惯了就快了,还是那句话在哪里就往反向转
 30   var y:longint;
 31   begin
 32     while fa[x]<>0 do
 33     begin
 34       y:=fa[x];
 35       if fa[y]=0 then
 36       begin
 37         if son[y,1]=x then rotate(x,2)
 38         else rotate(x,1);
 39       end
 40       else begin
 41         if son[fa[y],1]=y then
 42         begin
 43           if son[y,1]=x then
 44           begin
 45             rotate(y,2);
 46             rotate(x,2);
 47           end
 48           else begin
 49             rotate(x,1);
 50             rotate(x,2);
 51           end;
 52         end
 53         else begin
 54           if son[y,1]=x then
 55           begin
 56             rotate(x,2);
 57             rotate(x,1);
 58           end
 59           else begin
 60             rotate(y,1);
 61             rotate(x,1);
 62           end;
 63         end;
 64       end;
 65     end;
 66     root:=x;
 67   end;
 68 
 69 procedure delete(x:longint);    //删除写得比较丑陋
 70   var p,y,q,u:longint;
 71   begin
 72     y:=fa[x];
 73     if y=0 then q:=0
 74     else if son[y,1]=x then q:=1
 75     else if son[y,2]=x then q:=2;
 76     if (son[x,1]<>0) and (son[x,2]<>0) then
 77     begin
 78       p:=succ(x);
 79       if (son[p,2]<>0) and (fa[p]<>x) then
 80       begin
 81         son[fa[p],1]:=son[p,2];
 82         fa[son[p,2]]:=fa[p];
 83       end
 84       else if fa[p]<>x then son[fa[p],1]:=0;
 85       son[p,1]:=son[x,1];
 86       fa[son[x,1]]:=p;
 87       if fa[p]<>x then
 88       begin
 89         son[p,2]:=son[x,2];
 90         fa[son[x,2]]:=p;
 91       end;
 92       if y<>0 then son[y,q]:=p;
 93       fa[p]:=y;
 94       splay(p);
 95     end
 96     else begin
 97       if y<>0 then
 98       begin
 99         if son[x,1]<>0 then
100         begin
101           son[y,q]:=son[x,1];
102           fa[son[x,1]]:=y;
103         end;
104         if son[x,2]<>0 then
105         begin
106           son[y,q]:=son[x,2];
107           fa[son[x,2]]:=y;
108         end;
109       end;
110       if son[x,1]<>0 then u:=son[x,1] else u:=son[x,2];
111       if y=0 then
112       begin
113         root:=u;
114         fa[u]:=0;
115       end
116       else splay(y);
117     end;
118     dec(h);
119     fa[x]:=0;
120     son[x,1]:=0;
121     son[x,2]:=0;
122     d[x]:=0;
123   end;
124 
125 procedure insert(x:longint);
126   var p:longint;
127   begin
128     inc(t);
129     inc(h);
130     d[t]:=x;
131     if h=1 then
132     begin
133       root:=1;
134       fa[t]:=0;
135     end
136     else begin
137       p:=root;
138       repeat
139         if d[p]>=x then
140         begin
141           if son[p,1]=0 then break;
142           p:=son[p,1];
143         end
144         else begin
145           if son[p,2]=0 then break;
146           p:=son[p,2];
147         end;
148       until false;
149       fa[t]:=p;
150       if d[p]>=x then son[p,1]:=t else son[p,2]:=t;
151       splay(t);
152     end;
153   end;
154 
155 procedure swap(var a,b:longint);
156   var c:longint;
157   begin
158     c:=a;
159     a:=b;
160     b:=c;
161   end;
162 
163 procedure sortc(l,r:longint);
164   var i,j,x,y: longint;
165   begin
166     i:=l;
167     j:=r;
168     x:=cv[(l+r) shr 1];
169     y:=cw[(l+r) shr 1];
170     repeat
171       while (cv[i]<x) or ((cv[i]=x) and (cw[i]<y)) do inc(i);
172       while (x<cv[j]) or ((cv[j]=x) and (cw[j]>y)) do dec(j);
173       if not(i>j) then
174       begin
175         swap(cv[i],cv[j]);
176         swap(cw[i],cw[j]);
177         inc(i);
178         j:=j-1;
179       end;
180     until i>j;
181     if l<j then sortc(l,j);
182     if i<r then sortc(i,r);
183   end;
184 
185 procedure sort(l,r:longint);
186   var i,j,x,y: longint;
187   begin
188     i:=l;
189     j:=r;
190     x:=v[(l+r) shr 1];
191     y:=w[(l+r) shr 1];
192     repeat
193       while (v[i]<x) or ((v[i]=x) and (w[i]<y)) do inc(i);
194       while (x<v[j]) or ((v[j]=x) and (w[j]>y)) do dec(j);
195       if not(i>j) then
196       begin
197         swap(w[i],w[j]);
198         swap(v[i],v[j]);
199         inc(i);
200         j:=j-1;
201       end;
202     until i>j;
203     if l<j then sort(l,j);
204     if i<r then sort(i,r);
205   end;
206 begin
207   readln(n,m);
208   for i:=1 to n do
209     readln(cw[i],cv[i]);
210   for i:=1 to m do
211     readln(w[i],v[i]);
212   if m<n then
213   begin
214     writeln(-1);
215     halt;
216   end;
217   sortc(1,n);
218   sort(1,m);
219   j:=m;
220   t:=0;
221   root:=0;
222   fillchar(son,sizeof(son),0);
223   fillchar(fa,sizeof(fa),0);
224   for i:=n downto 1 do
225   begin
226     while v[j]>=cv[i] do
227     begin
228       insert(w[j]);
229       dec(j);
230     end;
231     insert(cw[i]);
232     p:=succ(t);
233     if p<>0 then
234     begin
235       ans:=ans+d[p];
236       delete(t);
237       delete(p);
238     end
239     else begin
240       ans:=-1;
241       break;
242     end;
243   end;
244   writeln(ans);
245 end.
View Code

话说splay终于写对了还是1Y,好高兴

 



 

posted on 2014-04-01 21:25  acphile  阅读(227)  评论(0编辑  收藏  举报