0% found this document useful (0 votes)
4 views2 pages

Dapnui

The document contains a Pascal program that processes an array of integers to calculate a minimum value based on specific conditions. It uses dynamic programming to compute two arrays, dp and h, which track the necessary adjustments to the input values. The final result is written to an output file after evaluating the minimum adjustments needed for the entire array.

Uploaded by

mathway319
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views2 pages

Dapnui

The document contains a Pascal program that processes an array of integers to calculate a minimum value based on specific conditions. It uses dynamic programming to compute two arrays, dp and h, which track the necessary adjustments to the input values. The final result is written to an output file after evaluating the minimum adjustments needed for the entire array.

Uploaded by

mathway319
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

const

maxn = 100005;
inf64 = 7000000000000000000;

var
a: array[1..maxn] of int64;
dp: array[0..1, 1..maxn] of int64;
h: array[0..1, 1..maxn] of int64;
n, i: longint;
f, res: int64;
fi, fo: text;

function max(a, b: int64): int64;


begin
if a > b then exit(a)
else exit(b);
end;

function min(a, b: int64): int64;


begin
if a < b then exit(a)
else exit(b);
end;

begin
assign(fi, 'dapnui.inp'); reset(fi);
assign(fo, 'dapnui.out'); rewrite(fo);

readln(fi, n);
for i := 1 to n do
read(fi, a[i]);

dp[0][1] := 0;
h[0][1] := a[1];
for i := 2 to n do
begin
if a[i] <= h[0][i-1] then
begin
dp[0][i] := dp[0][i-1] + (h[0][i-1] - a[i] + 1);
h[0][i] := h[0][i-1] + 1;
end
else
begin
dp[0][i] := dp[0][i-1];
h[0][i] := a[i];
end;
end;

dp[1][n] := 0;
h[1][n] := a[n];
for i := n-1 downto 1 do
begin
if a[i] <= h[1][i+1] then
begin
dp[1][i] := dp[1][i+1] + (h[1][i+1] - a[i] + 1);
h[1][i] := h[1][i+1] + 1;
end
else
begin
dp[1][i] := dp[1][i+1];
h[1][i] := a[i];
end;
end;

res := inf64;
for i := 2 to n-1 do
begin
if max(h[0][i-1], h[1][i+1]) < a[i] then
f := 0
else
f := max(h[0][i-1], h[1][i+1]) + 1 - a[i];
res := min(res, dp[0][i-1] + dp[1][i+1] + f);
end;

writeln(fo, res);

close(fi);
close(fo);
end.

You might also like