NOIP2002 提高组
[NOIP2002] 提高组
T1.均分纸牌
算法:贪心(模拟)
【分析】:
1.简化 2.过滤 3.辩证法 详见课件的例7
还有一种类似的思路是:求出平均值后,i←1 to n-1扫描,若a[i]与平均值不等则step+1,再把差值累加到后一堆(移动纸牌 a[i+1]+a[i]-average)
1 var 2 n,i,j,ave,step:longint; 3 a:array[1..100] of longint; 4 begin 5 assign(input,'jfzp.in'); 6 reset(input); 7 assign(output,'jfzp.out'); 8 rewrite(output); 9 ave:=0; 10 readln(n); 11 for i:=1 to n do 12 begin 13 read(a[i]); 14 inc(ave,a[i]); 15 end; 16 ave:=ave div n; 17 for i:=1 to n do a[i]:=a[i]-ave; 18 i:=1; j:=n; 19 while (a[i]=0) and (i<n) do inc(i); 20 while (a[j]=0) and (j>1) do dec(j); 21 step:=0; 22 while i<j do 23 begin 24 inc(a[i+1],a[i]); 25 a[i]:=0; 26 inc(step); 27 while (a[i]=0) and (i<j) do inc(i); 28 end; 29 writeln(step); 30 close(input); 31 close(output); 32 end.