题目:http://www.lydsy.com/JudgeOnline/problem.php?id=1572
这个是中文的,就不写大意了
这道题很水,大家有目共睹,就是一道贪心+堆排序。
按照时间从后往前,每个时间取出最大的那个值。可以加一些小小的优化什么的。
但是我居然先
WA了无数遍+TLE无数遍+RUNTIMEERROR无数遍+AC一次
WA是因为在取出最大值时有一些小小的问题。
TLE是我脑残的用了fillchar并且把不需要int64的数组设为了int64!
然后runtimeerroe则是我把int64改成longint时,函数返回值没改!
蒟蒻就是我!
代码:
var
j,last,n,i,f,t:longint;
ans:int64;
q,d,p:array[-10..100010] of longint;
procedure swap(var a,b:longint);
var
c:longint;
begin
c:=a;
a:=b;
b:=c;
end;
procedure try;
var
now:longint;
begin
now:=t;
while (now>1) and (q[now div 2]<q[now]) do
begin
swap(q[now],q[now div 2]);
now:=now div 2;
end;
end;
function work:int64;
var
now,j:longint;
begin
if t<1 then exit(0);
work:=q[1];
swap(q[1],q[t]);
q[t]:=0;
t:=t-1;
now:=1;
while (now<t) and ((now*2)<=t) and ((q[now*2]>q[now]) or (q[now*2+1]>q[now])) do
begin
if ((now*2+1)>t) then j:=now*2 else
if q[now*2]<q[now*2+1] then j:=now*2+1 else j:=now*2;
swap(q[now],q[j]);
now:=j;
end;
end;
procedure sort(l,r: longint);
var
i,j,x,y: longint;
begin
i:=l;
j:=r;
x:=d[(l+r) div 2];
repeat
while d[i]<x do
inc(i);
while x<d[j] do
dec(j);
if not(i>j) then
begin
y:=d[i];
d[i]:=d[j];
d[j]:=y;
y:=p[i];
p[i]:=p[j];
p[j]:=y;
inc(i);
j:=j-1;
end;
until i>j;
if l<j then
sort(l,j);
if i<r then
sort(i,r);
end;
begin
read(n);
for i:=1 to n do
read(d[i],p[i]);
sort(1,n);
i:=n;
t:=0;
ans:=0;
last:=-1;
for i:=n downto 1 do
begin
f:=d[i];
if last=-1 then last:=f;
if (last-f)>=t then
begin
for j:=1 to t do
ans:=ans+q[j];
t:=0;
end
else
for j:=f+1 to last do
ans:=ans+work;
last:=f;
t:=t+1;
q[t]:=p[i];
try;
end;
if (last-f)>=t then
begin
for j:=1 to t do
ans:=ans+q[j];
t:=0;
end
else
for j:=1 to last do
ans:=ans+work;
writeln(ans);
end.