Balancer - CodeForces 440B
来源:互联网 发布:移动教学软件 编辑:程序博客网 时间:2024/06/03 19:55
Description
Petya has k matches, placed in n matchboxes lying in a line from left to right. We know that k is divisible by n. Petya wants all boxes to have the same number of matches inside. For that, he can move a match from its box to the adjacent one in one move. How many such moves does he need to achieve the desired configuration?
Input
The first line contains integer n (1 ≤ n ≤ 50000). The second line contains n non-negative numbers that do not exceed 109, the i-th written number is the number of matches in the i-th matchbox. It is guaranteed that the total number of matches is divisible by n.
Output
Print the total minimum number of moves.
Sample Input
61 6 2 5 3 7
12
题意:一步步的移火柴使火柴盒里的火柴数相等所需要的最短步数。
思路:第一个不够的都从第二个拿,第二个不够的都从第三个拿,出现负数没关系,可以先后面拿过来。
AC代码:
import java.util.*;public class Main {public static void main(String[] args) {Scanner scan=new Scanner(System.in);int n=scan.nextInt();long m[]=new long[n];long sum=0;for(int i=0;i<n;i++){m[i]=scan.nextLong();sum+=m[i];}long av=sum/n;for(int i=0;i<n;i++){m[i]-=av;}long ans=Math.abs(m[0]);for(int i=1;i<n;i++){m[i]+=m[i-1];ans+=Math.abs(m[i]);}System.out.println(ans);}}
- Balancer - CodeForces 440B
- Balancer - CodeForces 440B
- codeforces B. Balancer
- Codeforces Testing Round #10 B. Balancer
- codeforces B
- codeforces B
- codeforces B
- codeforces B
- hadoop balancer hbase balancer
- Codeforces Round #440 (Div. 2) A B
- CodeForces 626B CodeForces 626B【暴力】
- CodeForces 841B (B) 博弈
- codeforces 134B
- codeforces#98 b
- codeforces 105 div2 B
- Codeforces 166B - Polygons
- codeforces B. Coins
- codeforces----193B Xor
- HighCharts实现多数据折线图分列显示
- 怎么实现Web聊天
- 数据结构之并查集
- dhtmlxgrid数据表格在ie9,ie10下无法隐藏列
- 关于Oracle分页SQL
- Balancer - CodeForces 440B
- Ordering Tasks
- C++以阻塞的方式调用外部exe程序,等待其运行结束
- C开源hash代码uthash的用法总结(1)
- 百度有道雅虎的实习面试经历
- URL的组成
- 如何注册一个新的form到EBS系统
- ArcGIS教程:河网分级的工作原理
- 北邮新生排位赛2解题报告a-c