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 ...
随机推荐
- 自定义View绘制字符串
import android.app.Activity; import android.os.Bundle; import android.view.Display; import android.v ...
- 已经被cocos2dx给折腾的想要放弃它,专注Unity3D的怀抱了!
一直使用cocos2dx编写自己的2D小游戏,不得不说,编写个人的超级小规模的游戏,使用cocos2dx有一定的优势,首先门槛很低,编写2D游戏用起来也算顺手,可惜一直没有一个优秀的UI编辑器,好不容 ...
- openGl从零开始之添加颜色
OpenGL 支持两种颜色模式:一种是 RGBA模式,一种是 颜色索引模式.无论哪种颜色模式,计算机都必须为每一个像素保存一些数据,即通过每一个像素的颜色,来改变整体图形的颜色.不同的是, RGBA ...
- Shapefile文件中的坐标绘制到屏幕时的映射模式设置
pDC->SetMapMode(MM_ANISOTROPIC ); //首先选择MM_ANISOTROPIC映射模式,其它映射模式都不合适 pDC->SetWindowExt( max(a ...
- scala: How to write a simple HTTP GET request client in Scala (with a timeout)
Scala CookBook: http://scalacookbook.com/ @throws(classOf[java.io.IOException]) @throws(classOf[java ...
- 一排div自由下落
function getstyle(obj,attr) { return obj.currentStyle?obj.currentStyle[attr]:getComputedStyle(obj)[a ...
- Spark RDD概念学习系列之RDD的操作(七)
RDD的操作 RDD支持两种操作:转换和动作. 1)转换,即从现有的数据集创建一个新的数据集. 2)动作,即在数据集上进行计算后,返回一个值给Driver程序. 例如,map就是一种转换,它将数据集每 ...
- android错误系列之导出数据库出错Failed to pull selection
使用效率检视工具traceView,在导出检测文件时,出现了“failed to pull a selection”问题,网上搜索了几篇文章,有的说,是因为导出超时,我将windows-->pr ...
- TcxDBVerticalGrid优秀的编辑控件
- PyQt多窗口调用
经常有人问到如何在一个主窗口中打开一个对话框,然后在确认对话框之后,开启另一个窗口进行后续操作,要求主窗口和最终的窗口之间都能响应用户操作,也就是非模态窗口.随手写了几行代码,简要示意. :::pyt ...