您的位置:首页 > 其它

wikioi 1098 均分纸牌

2014-08-05 19:34 267 查看

题目描述 Description


有 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)。




输入描述 Input Description



第一行N(N 堆纸牌,1 <= N <= 100)

第二行A1 A2 … An (N 堆纸牌,每堆纸牌初始数,l<= Ai <=10000)




输出描述 Output Description



输出至屏幕。格式为:

所有堆均达到相等时的最少移动次数。‘




样例输入 Sample Input



4

9 8 17 6




样例输出 Sample Output



3

//这是一道基础的贪心问题.
#include<iostream>
#include<string.h>
#include<stdio.h>
#include<ctype.h>
#include<algorithm>
#include<stack>
#include<queue>
#include<set>
#include<math.h>
#include<vector>
#include<map>
#include<deque>
#include<list>
using namespace std;
int a[1000];
int main()
{
int n,sum=0;
cin>>n;
for(int i=0;i<n;i++)
{
cin>>a[i];
sum+=a[i];
}// 计算总和
int p=sum/n,step=0;//计算平均数
for(int i=0;i<n-1;i++)
{
int t=0;
if(a[i]!=p)
{
t=a[i]-p;
a[i+1]+=t;
step++;
}//总是像右边索取,不论多少,均从右边拿. 满足局部最优解
}
cout<<step;
return 0;
}


View Code

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: