题解:HNOI2002 营业额统计

题目描述

Tiger最近被公司升任为营业部经理,他上任后接受公司交给的第一项任务便是统计并分析公司成立以来的营业情况。

Tiger拿出了公司的账本,账本上记录了公司成立以来每天的营业额。分析营业情况是一项相当复杂的工作。由于节假日,大减价或者是其他情况的时候,营业额会出现一定的波动,当然一定的波动是能够接受的,但是在某些时候营业额突变得很高或是很低,这就证明公司此时的经营状况出现了问题。经济管理学上定义了一种最小波动值来衡量这种情况:

当最小波动值越大时,就说明营业情况越不稳定。

而分析整个公司的从成立到现在营业情况是否稳定,只需要把每一天的最小波动值加起来就可以了。你的任务就是编写一个程序帮助Tiger来计算这一个值。

第一天的最小波动值为第一天的营业额。

该天的最小波动值=min{|该天以前某一天的营业额-该天营业额|}。

解题思路:

这道题可以用离线链表解决,先一次性读到最后一个,再根据读入的顺序删除加进来的高度

 1var
2    answer,height,previous,next:array[0..40005of longint;
3    rank:array[0..40005of longint;
4    n,higher,shorter,i,maxn,sum:longint;
5procedure sort(l,r:longint);
6var
7    x,i,j,tmp:longint;
8begin
9    x:=height[rank[(l+r) div 2]];i:=l;j:=r;
10    while i<=j do
11    begin
12        while height[rank[i]] < x do
13            inc(i);
14        while height[rank[j]] > x do
15            dec(j);
16        if  i<=j then
17        begin
18            //swap(rank[i],rank[j]);
19            tmp:=rank[i];
20            rank[i]:=rank[j];
21            rank[j]:=tmp;
22            inc(i); dec(j);
23        end;
24    end;
25    if i<r then sort(i,r);
26    if l<j then sort(l,j);
27end;
28begin
29    maxn:=40005;
30    readln(n);
31    for i:=1 to n do
32    begin
33        read(height[i]);
34        rank[i]:=i;
35    end;
36    sort(1,n);
37    for i:=1 to n do
38    begin
39        previous[rank[i]]:=rank[i-1];
40        next[rank[i]]:=rank[i+1];
41    end;
42    for i:=n downto 2 do
43    begin
44        higher:=1023456789; shorter:=1023456789;
45        if previous[i]<>0 then
46            shorter:=height[i] - height[previous[i]];
47        if next[i]<>0 then
48            higher:=height[next[i]]-height[i];
49        if shorter < higher then
50            answer[i]:=previous[i] else
51            answer[i]:=next[i];
52        next[previous[i]]:=next[i];
53        previous[next[i]]:=previous[i];
54    end;
55    sum:=height[1];
56    for i:=2 to n do
57        sum:=sum+(abs(height[i]-height[answer[i]]));
58    //    sum:=sum+answer[i];
59    //writeln(sum);
60    writeln(sum);
61end.

猜你喜欢

转载自www.cnblogs.com/titititing/p/9630675.html