记录编号 |
30175 |
评测结果 |
AAAAAAAAAA |
题目名称 |
抢修道路 |
最终得分 |
100 |
用户昵称 |
reamb |
是否通过 |
通过 |
代码语言 |
Pascal |
运行时间 |
0.497 s |
提交时间 |
2011-10-27 20:55:13 |
内存使用 |
15.40 MiB |
显示代码纯文本
program roady;
var
f:array[0..2000,0..2000]of longint;
i,j,n,ans:longint;
a,b:array[1..2000]of longint;
procedure swap(var a,b:longint);
var
x:longint;
begin
x:=a;
a:=b;
b:=x
end;
procedure sort(l,r: longint);
var
i,j,x,y: longint;
begin
i:=l;
j:=r;
x:=a[(l+r) div 2];
repeat
while a[i]<x do
inc(i);
while x<a[j] do
dec(j);
if not(i>j) then
begin
y:=a[i];
a[i]:=a[j];
a[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
assign (input,'roady.in');
reset (input);
assign (output,'roady.out');
rewrite (output);
readln (n);
for i:=1 to n do
begin
read (a[i]);
b[i]:=a[i]
end;
sort(1,n);
for i:=1 to n do
for j:=0 to n do
f[i,j]:=maxlongint;
for i:=1 to n do
f[0,i]:=0;
for i:=1 to n do
for j:=1 to n do
begin
f[i,j]:=f[i,j-1];
if f[i-1,j]+abs(b[i]-a[j])<f[i,j] then
f[i,j]:=f[i-1,j]+abs(b[i]-a[j])
end;
ans:=f[n,n];
for i:=1 to n div 2 do
swap(b[i],b[n-i+1]);
for i:=1 to n do
for j:=0 to n do
f[i,j]:=maxlongint;
for i:=1 to n do
f[0,i]:=0;
for i:=1 to n do
for j:=1 to n do
begin
f[i,j]:=f[i,j-1];
if f[i-1,j]+abs(b[i]-a[j])<f[i,j] then
f[i,j]:=f[i-1,j]+abs(b[i]-a[j])
end;
if f[n,n]<ans then
ans:=f[n,n];
writeln (ans);
close (input);
close (output)
end.