[NOIP2002] 提高组

T1.均分纸牌

算法贪心(模拟)

【分析】:

1.简化 2.过滤 3.辩证法  详见课件的例7

还有一种类似的思路是:求出平均值后,i←1 to n-1扫描,若a[i]与平均值不等则step+1,再把差值累加到后一堆(移动纸牌 a[i+1]+a[i]-average)

 var
n,i,j,ave,step:longint;
a:array[..] of longint;
begin
assign(input,'jfzp.in');
reset(input);
assign(output,'jfzp.out');
rewrite(output);
ave:=;
readln(n);
for i:= to n do
begin
read(a[i]);
inc(ave,a[i]);
end;
ave:=ave div n;
for i:= to n do a[i]:=a[i]-ave;
i:=; j:=n;
while (a[i]=) and (i<n) do inc(i);
while (a[j]=) and (j>) do dec(j);
step:=;
while i<j do
begin
inc(a[i+],a[i]);
a[i]:=;
inc(step);
while (a[i]=) and (i<j) do inc(i);
end;
writeln(step);
close(input);
close(output);
end.

我的程序

05-11 22:24