寒假篇39
来源:互联网 发布:临沂拓普网络拖欠工资 编辑:程序博客网 时间:2024/05/16 09:52
题目描述
有 N 堆纸牌,编号分别为 1,2,…, N。每堆上有若干张,但纸牌总数必为 N 的倍数。可以在任一堆上取若于张纸牌,然后移动。
移牌规则为:在编号为 1 堆上取的纸牌,只能移到编号为 2 的堆上;在编号为 N 的堆上取的纸牌,只能移到编号为 N-1 的堆上;其他堆上取的纸牌,可以移到相邻左边或右边的堆上。
现在要求找出一种移动方法,用最少的移动次数使每堆上纸牌数都一样多。
例如 N=4,4 堆纸牌数分别为:
①9②8③17④6
移动3次可达到目的:
从 ③ 取 4 张牌放到 ④ (9 8 13 10) -> 从 ③ 取 3 张牌放到 ②(9 11 10 10)-> 从 ② 取 1 张牌放到①(10 10 10 10)。
输入输出格式
输入格式:
键盘输入文件名。文件格式:
N(N 堆纸牌,1 <= N <= 100)
A1 A2 … An (N 堆纸牌,每堆纸牌初始数,l<= Ai <=10000)
输出格式:
输出至屏幕。格式为:
所有堆均达到相等时的最少移动次数。
输入输出样例
输入样例#1:
4
9 8 17 6
输出样例#1:
3
做法:贪心,事实上,在除去所有的刚好的之后可以一堆一堆的直接做
代码如下:
var n,i,s,j,ans:longint; a:array[0..1000] of longint;begin readln(n); for i:=1 to n do begin read(a[i]); s:=s+a[i]; end; s:=s div n; for i:=1 to n do a[i]:=a[i]-s; i:=1; j:=n; while (a[i]=0)and(i<n)do inc(i); while (a[j]=0)and(j>1) do dec(j); while i<j do begin a[i+1]:=a[i+1]+a[i]; a[i]:=0; while (a[i]=0)and(i<n) do inc(i); inc(ans); end; writeln(ans);end.
0 0
- 寒假篇39
- 寒假篇 -1
- 寒假篇 -2
- 寒假篇 - 3
- 寒假前篇
- 寒假篇12-笨小猴
- 寒假篇14
- 寒假篇18
- 寒假篇19
- 寒假篇20
- 寒假篇21
- 寒假篇22
- 寒假篇23
- 寒假篇24
- 寒假篇25
- 寒假篇26
- 寒假篇27
- 寒假篇28
- 17 - 05 - 16 HTML( <div> 标签)
- poj3619
- NodeJS常用模块介绍
- poj3632
- poj3637
- 寒假篇39
- 母函数 初步刷题
- android:layout_gravity 和 android:gravity 的区别
- poj3650
- 洛谷 P1201 [USACO1.1]贪婪的送礼者Greedy Gift Givers
- spring不通过注入的方式获取对象(BeanFactoryAware \ApplicationContextAware )
- poj3664
- 台阶问题
- poj3665