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 ...
随机推荐
- MSSQL 查询分组前N条记录
sql语句中,查询分组中前n条记录的sql语句如下 第一种方法 select * from consultingfeebill awhere n>(select count(*) from co ...
- vector.resize 与 vector.reserve的区别 .xml
pre{ line-height:1; color:#9f1d66; background-color:#a0ffc0; font-size:16px;}.sysFunc{color:#5d57ff; ...
- Python 最佳实践
前言 对我来说,以前每次面试是我审视自己,检验自己的一种方式.每次准备面试,以及被面试官问住的时候才会发现,其实我python我学的还不够好.工作中也是,可以从其他的同事那里获得成长.但是我今天说的是 ...
- Cloudera Manager5安装总结遇到问题及解决办法
安装过程中,由于网络终端,导致下面问题: 问题1:安装停止在获取安装锁/tmp/scm_prepare_node.tYlmPfrT using SSH_CLIENT to get the SCM ho ...
- Flex3.0 Loader类的练习
<?xml version="1.0" encoding="utf-8"?> <mx:Application xmlns:mx="h ...
- Nodejs开发指南-笔记
第三章 异步式I/O与事件编程3.1 npm install -g supervisor supervisor app.js 当后台修改代码后,服务器自动重启,生效修改的代码,不用手动停止/启动3.2 ...
- 使用Ambari部署hadoop集群
准备工作 1. 设置sudo免密码登陆 由于CentOS安装之后,普通用户无sudo权限,故应该设置sudo权限. 参考文章:http://www.cnblogs.com/maybob/p/32988 ...
- c语言分析函数调用关系图(call graph)的几种方法
一.基于 Doxygen或 lxr 的API形式的文档系统. 二.基于CodeViz, CodeViz是<Understanding The Linux Virtual Memory Manag ...
- 转自 处理老版PIL 到 pillow
帮新同事部署开发环境, 由于项目代码里用到了PIL库处理图片, 导致一些图片在浏览器中无法正常显示. 几番折腾, 解决了问题, 这里记录一下报的问题, 及解决方法: 1. python版本不对, 6 ...
- 第三百五十九天 how can I 坚持
在家待了一天,鼓捣了下linux,总算能连上网了,懂得还是少啊. 晚上去华北电力大学跑了会步,十圈,还挺有成就感呢,就是没带手环,哎. 以后学习一定要记笔记,上了这么多年学,都 没学会怎么记笔记,也是 ...