P1572: [Usaco2009 Open]工作安排Job

时间:2022-07-10 20:17:52

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

 var n,i,j,l,r,min,mpos:longint;
ans:int64;
d,p,q:array[..] of longint;
procedure swap(var a,b:longint);
var tem:longint;
begin
tem:=a;
a:=b;
b:=tem;
end;
procedure qs(t,w:longint);
var mid1,mid2,l,r:longint;
begin
l:=t; r:=w;
mid1:=d[(l+r) shr ]; mid2:=p[(l+r) shr ];
repeat
begin
while (d[l]<mid1) or ((d[l]=mid1) and (p[l]<mid2)) do inc(l);
while (d[r]>mid1) or ((d[r]=mid1) and (p[r]>mid2)) do dec(r);
if l<=r then
begin
swap(d[l],d[r]);
swap(p[l],p[r]);
inc(l); dec(r);
end;
end;
until l>r;
if t<r then qs(t,r);
if l<w then qs(l,w);
end;
begin
readln(n);
for i:= to n do
readln(d[i],p[i]);
qs(,n);
{nowd:=-;
for i:= to n do
begin
if nowd<>d[i] then
begin
nowd:=d[i];
inc(ans,p[i]);
end;
end;}
l:=; r:=; q[]:=p[];
for i:= to n do
begin
min:=maxlongint;
if r-l+<d[i] then
begin
inc(r);
q[r]:=p[i];
end
else
begin
for j:=l to r do
if min>q[j] then
begin
min:=q[j];
mpos:=j;
end;
if p[i]>min then
q[mpos]:=p[i];
end;
end;
for i:= to r do inc(ans,q[i]);
writeln(ans);
end.

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