CodeForces--609C --Load Balancing(水题)
Time Limit: 2000MS | Memory Limit: 262144KB | 64bit IO Format: %I64d & %I64u |
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
2
7
10 11 10 11 10 11 11
0
5
1 2 3 4 5
3
Sample Output
Hint
In the first example two seconds are needed. In each second, a single task from server #2 should be moved to server #1. After two seconds there should be 3 tasks on server #1 and 4 tasks on server #2.
In the second example the load is already balanced.
A possible sequence of task movements for the third example is:
- move a task from server #4 to server #1 (the sequence
m becomes: 2 2 3 3 5); - then move task from server #5 to server #1 (the sequence
m becomes: 3 2 3 3 4); - then move task from server #5 to server #2 (the sequence
m becomes: 3 3 3 3 3).
The above sequence is one of several possible ways to balance the load of servers in three seconds.
Source
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int num[1000010];
int main()
{
int n;
scanf("%d",&n);
int sum=0;
int maxx,minn;
for(int i=0;i<n;i++)
{
scanf("%d",&num[i]);
sum+=num[i];
}
minn=maxx=sum/n;
int ans1,ans2;
ans1=ans2=0;
if(sum%n)
maxx++;
for(int i=0;i<n;i++)
{
if(num[i]<minn) ans1+=minn-num[i];
else if(maxx<num[i]) ans2+=num[i]-maxx;
}
int s=max(ans1,ans2);
printf("%d\n",s);
return 0;
}
CodeForces--609C --Load Balancing(水题)的更多相关文章
- CodeForces 609C Load Balancing
先算出目标状态,然后拿当前状态与目标状态对比,即可算出答案 #include<cstdio> #include<cmath> #include<cstring> # ...
- Codeforces Gym 100531G Grave 水题
Problem G. Grave 题目连接: http://codeforces.com/gym/100531/attachments Description Gerard develops a Ha ...
- codeforces 706A A. Beru-taxi(水题)
题目链接: A. Beru-taxi 题意: 问那个taxi到他的时间最短,水题; AC代码: #include <iostream> #include <cstdio> #i ...
- codeforces 569B B. Inventory(水题)
题目链接: B. Inventory time limit per test 1 second memory limit per test 256 megabytes input standard i ...
- Codeforces 489A SwapSort (水题)
A. SwapSort time limit per test 1 second memory limit per test 256 megabytes input standard input ou ...
- codeforces 688A A. Opponents(水题)
题目链接: A. Opponents time limit per test 1 second memory limit per test 256 megabytes input standard i ...
- CodeForces 534B Covered Path (水题)
题意:给定两个速度,一个一初速度,一个末速度,然后给定 t 秒时间,还每秒速度最多变化多少,让你求最长距离. 析:其实这个题很水的,看一遍就知道怎么做了,很明显就是先从末速度开始算起,然后倒着推. 代 ...
- Codeforces Gym 100286I iSharp 水题
Problem I. iSharpTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/ ...
- CodeForces 705A(训练水题)
题目链接:http://codeforces.com/problemset/problem/705/A 从第三个输出中可看出规律, I hate that I love that I hate it ...
随机推荐
- 利用JavaScript制作简易日历
<html xmlns="http://www.w3.org/1999/xhtml" lang="en"> <head> <met ...
- javascript中的原型对象
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- smarty 3 + codeigniter 2 + hmvc
参考资料 https://bitbucket.org/wiredesignz/codeigniter-modular-extensions-hmvc/src/fecd39ccdf56?at=defau ...
- Qwt--散点图/函数图
1.Qwt库 QwtPlot是用来绘制二维图像的widget.在它的画板上可以无限制的显示绘画组件.绘画组件可以是曲线(QwtPlotCurve).标记(QwtPlotMarker).网格(QwtPl ...
- 03--软件包管理工具 apt
APT APT(the Advanced Packaging Tool)是Ubuntu 软件包管理系统的高级界面,由几个名字以“apt-”打头的程序组成.apt-get.apt-cache ...
- gulp创建完整的项目流程
所有的环境都是在 node 安装好的基础上执行的. node -v 查看node的安装情况.npm -v查看npm 的安装情况. gulp自动化构建常用参数 1.src 读取文件或者文件夹 2.des ...
- vue .sync修饰符
.sync 修饰符 对一个 prop 进行“双向绑定”时,真正的双向绑定会带来维护上的问题,因为子组件可以修改父组件,且在父组件和子组件都没有明显的改动来源. 那这个修饰符的原理是什么呢?其实还是vu ...
- 【JavaScript框架封装】公共框架的封装
/* * @Author: 我爱科技论坛 * @Time: 20180706 * @Desc: 实现一个类似于JQuery功能的框架 // 公共框架 // 种子模块:命名空间.对象扩展.数组化.类型的 ...
- VS2008集成QT的OpenGL开发(实现二维图形的旋转)
主要是利用Qt中的定时器实现了二维图形的旋转功能: #ifndef QGLTEST_H #define QGLTEST_H #include <QGLWidget> #include &l ...
- 理解 Javascript 执行上下文和执行栈
如果你是一名 JavaScript 开发者,或者想要成为一名 JavaScript 开发者,那么你必须知道 JavaScript 程序内部的执行机制.理解执行上下文和执行栈同样有助于理解其他的 Jav ...