P1572: [Usaco2009 Open]工作安排Job

做这道题走了不少弯路,其实本身也是很简单的,类似单调队列的东西。刚开始以为双关键字排序就行了,结果连WA两遍,忽然意识到可以在截止之前做这件事!!于是就规规矩矩的打队列,然而忘记找队列里的最小P做,当成是单调的了,于是又顺顺利利地WA了好多次了,说多了都是泪啊。。

 1 var n,i,j,l,r,min,mpos:longint;
 2 ans:int64;
 3 d,p,q:array[0..100000] of longint;
 4 procedure swap(var a,b:longint);
 5 var tem:longint;
 6 begin
 7   tem:=a;
 8   a:=b;
 9   b:=tem;
10 end;
11 procedure qs(t,w:longint);
12 var mid1,mid2,l,r:longint;
13 begin
14   l:=t; r:=w;
15   mid1:=d[(l+r) shr 1]; mid2:=p[(l+r) shr 1];
16   repeat
17     begin
18       while (d[l]<mid1) or ((d[l]=mid1) and (p[l]<mid2)) do inc(l);
19       while (d[r]>mid1) or ((d[r]=mid1) and (p[r]>mid2)) do dec(r);
20       if l<=r then
21         begin
22           swap(d[l],d[r]);
23           swap(p[l],p[r]);
24           inc(l); dec(r);
25         end;
26     end;
27   until l>r;
28   if t<r then qs(t,r);
29   if l<w then qs(l,w);
30 end;
31 begin
32   readln(n);
33   for i:=1 to n do
34     readln(d[i],p[i]);
35   qs(1,n);
36   {nowd:=-1;
37   for i:=1 to n do
38     begin
39       if nowd<>d[i] then
40         begin
41           nowd:=d[i];
42           inc(ans,p[i]);
43         end;
44     end;}
45   l:=1; r:=1; q[1]:=p[1];
46   for i:=2 to n do
47     begin
48       min:=maxlongint;
49       if r-l+1<d[i] then
50         begin
51           inc(r);
52           q[r]:=p[i];
53         end
54       else
55         begin
56           for j:=l to r do
57             if min>q[j] then
58               begin
59                 min:=q[j];
60                 mpos:=j;
61               end;
62           if p[i]>min then
63             q[mpos]:=p[i];
64         end;
65     end;
66   for i:=1 to r do inc(ans,q[i]);
67   writeln(ans);
68 end.
View Code

 

 

(转载请注明出处:http://www.cnblogs.com/Kalenda/)

posted @ 2015-09-21 15:51  LovelyMonster丶  阅读(199)  评论(0编辑  收藏  举报