Codeforces Educational Codeforces Round 3 C. Load Balancing 贪心
C. Load Balancing
题目连接:
http://www.codeforces.com/contest/609/problem/C
Description
In the school computer room there are n servers which are responsible for processing several computing tasks. You know the number of scheduled tasks for each server: there are mi tasks assigned to the i-th server.
In order to balance the load for each server, you want to reassign some tasks to make the difference between the most loaded server and the least loaded server as small as possible. In other words you want to minimize expression ma - mb, where a is the most loaded server and b is the least loaded one.
In one second you can reassign a single task. Thus in one second you can choose any pair of servers and move a single task from one server to another.
Write a program to find the minimum number of seconds needed to balance the load of servers.
Input
The first line contains positive number n (1 ≤ n ≤ 105) — the number of the servers.
The second line contains the sequence of non-negative integers m1, m2, ..., mn (0 ≤ mi ≤ 2·104), where mi is the number of tasks assigned to the i-th server.
Output
Print the minimum number of seconds required to balance the load.
Sample Input
2
1 6
Sample Output
2
Hint
题意
有n个机器,每个机器的任务时间是a[i],你的任务是在最少的操作,使得所有Maxa[i]-Mina[i]最小。每次操作是可以使得某一个a[i]-1,某一个a[i]+1。
题解:
很显然,最后结果一定都在平均值位置,我们也可以比较轻松得到,最后究竟是多少个数为平均值,多少个数为平均值+1。所以我们直接贪心就好了。
小于平均值的就直接拉上去,大于平均值的就减下去就好了。
代码
#include<bits/stdc++.h>
using namespace std;
int a[100005];
int main()
{
int n;scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
sort(a+1,a+1+n);
long long sum = 0;
for(int i=1;i<=n;i++)
sum+=a[i];
int p = sum / n;
int n1 = n - sum%n;
int ans = 0;
for(int i=1;i<=n1;i++)
{
if(p<=a[i])break;
ans+=p-a[i];
}
for(int i=n1+1;i<=n;i++)
{
if(a[i]>p)break;
ans+=p+1-a[i];
}
printf("%d\n",ans);
}
Codeforces Educational Codeforces Round 3 C. Load Balancing 贪心的更多相关文章
- Codeforces Educational Codeforces Round 44 (Rated for Div. 2) F. Isomorphic Strings
Codeforces Educational Codeforces Round 44 (Rated for Div. 2) F. Isomorphic Strings 题目连接: http://cod ...
- Codeforces Educational Codeforces Round 44 (Rated for Div. 2) E. Pencils and Boxes
Codeforces Educational Codeforces Round 44 (Rated for Div. 2) E. Pencils and Boxes 题目连接: http://code ...
- CF# Educational Codeforces Round 3 C. Load Balancing
C. Load Balancing time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
- Educational Codeforces Round 3 C. Load Balancing
C. Load Balancing time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
- codeforces Educational Codeforces Round 5 A. Comparing Two Long Integers
题目链接:http://codeforces.com/problemset/problem/616/A 题目意思:顾名思义,就是比较两个长度不超过 1e6 的字符串的大小 模拟即可.提供两个版本,数组 ...
- codeforces Educational Codeforces Round 16-E(DP)
题目链接:http://codeforces.com/contest/710/problem/E 题意:开始文本为空,可以选择话费时间x输入或删除一个字符,也可以选择复制并粘贴一串字符(即长度变为两倍 ...
- Codeforces Educational Codeforces Round 15 E - Analysis of Pathes in Functional Graph
E. Analysis of Pathes in Functional Graph time limit per test 2 seconds memory limit per test 512 me ...
- Codeforces Educational Codeforces Round 15 D. Road to Post Office
D. Road to Post Office time limit per test 1 second memory limit per test 256 megabytes input standa ...
- Codeforces Educational Codeforces Round 15 C. Cellular Network
C. Cellular Network time limit per test 3 seconds memory limit per test 256 megabytes input standard ...
随机推荐
- 【LeetCode 235】Lowest Common Ancestor of a Binary Search Tree
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BS ...
- Cocos2d-android (04) 执行多个动作
先后.同时执行多个动作及动作序列执行结束后的事件 import org.cocos2d.actions.instant.CCCallFunc; import org.cocos2d.actions.i ...
- 使用xshell使用删除出现乱码
在使用xshell的时候出现如下错误: 1.打开如下属性设置: 2. 查看终端--键盘设置 4.修改为下图所示: 从而可解决删除乱码问题,在设置完成之后,关闭xshell,重新打开即可.
- 【2013微软面试题】输出节点数为n的二叉树的所有形态
转自:http://blog.csdn.net/monsterxd/article/details/8449005 /* * 题意,求节点数为n的二叉树的所有形态,先要想个方式来唯一标示一棵二叉树 ...
- JAVA分析html算法(JAVA网页蜘蛛算法)
近来有些朋友在做蜘蛛算法,或者在网页上面做深度的数据挖掘.但是遇到复杂而繁琐的html页面大家都望而却步.因为很难获取到相应的数据. 最古老的办法的是尝试用正则表达式,估计那么繁琐的东西得不偿失,浪费 ...
- U盘安装RedHat 5.3
转载自http://www.cnblogs.com/totozlj/archive/2012/06/03/2532757.html 1.下载rhel-5.3-server-i386-dvd.iso文件 ...
- openstack 实例迁移
- Codeforces Round #367 (Div. 2) B. Interesting drink (模拟)
Interesting drink 题目链接: http://codeforces.com/contest/706/problem/B Description Vasiliy likes to res ...
- cvc-elt.1: Cannot find the declaration of element 'beans'
@(编程) 现象描述 导入的一个eclipse项目报错,各种方法都无法解决,报错信息如下: cvc-elt.1: Cannot find the declaration of element 'bea ...
- 修改hosts文件解决OneDrive被墙的问题
增加如下内容就可以了.如果不知道修改hosts文件的具体方法请自行百度. 134.170.108.26 onedrive.live.com 134.170.108.152 skyapi.onedriv ...