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
相关文章推荐
- wikioi 1098 均分纸牌
- wikioi 1098 均分纸牌
- wikioi 1098 均分纸牌
- wikioi天梯之1098 均分纸牌 贪心
- wikioi-天梯-普及一等-贪心-1098:均分纸牌
- WIKIOI-1098 均分纸牌
- wikioi 1098 均分纸牌 (2002年NOIP全国联赛提高组)
- 【贪心】【codevs】1098 均分纸牌
- CODEVS 1098 均分纸牌(贪心)
- 【codevs 1098】均分纸牌
- CODE[VS]_1098 均分纸牌问题
- 1098 均分纸牌 ——http://codevs.cn/problem/1098/
- Codevs 1098 均分纸牌 贪心
- codevs 均分纸牌 1098
- codevs1098 均分纸牌
- [codevs] 1098 均分纸牌
- code[vs] 1098 均分纸牌
- codevs1098 均分纸牌
- codevs1098 均分纸牌(贪心)
- codevs 1098 均分纸牌