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 ...
随机推荐
- B - Spyke Talks
Problem description Polycarpus is the director of a large corporation. There are n secretaries worki ...
- html5和css3的笔记
h5+c3 W3C盒子模型和ie盒子模型 文档<!DOCTYPE html>加上的话,所有浏览器都按照W3C的盒子模型,否则ie会按照ie的盒子模型,它的content包括了padding ...
- Java基础8一面向对象
一.JavaBean标准的定义规范 1.类中所有的属性必须是私有的,也就是说属性必须用private修饰. 2.提供一个公共无参数的构造方法. 3.为所有私有的属性提供公共的set和get方法. se ...
- HTML实现图片360度循环旋转
<style> .header{ -webkit-animation:rotateImg 5s linear infinite;<!--修改旋转周期--> border: 1p ...
- oracle 编译包的时候,一直提示正在编译
select b.sid,b.serial#,b.machine,b.terminal,b.program,b.process,b.status from v$lock a , v$session b ...
- 00--C++牛人的博客
那些C++牛人的博客 这篇文章来自转载,转载的网址找不到了, 如果有人知道,可以在下面评论, 非常感谢,更感谢原作者. 现整理收集C++世界里那些“牛人”的个人博客.凡三类:一是令人高山仰止的 ...
- PHP 常用 数组函数
1:array_push($arr,'添加的值') 往数组里面添加元素2:array_unique($arr) 去重函数3:array_reverse($arr) 倒叙排列
- pycharm一些快捷键
1.鼠标方法在内建函数上,Ctrl+B,看内建函数的定义 2.pycharm单行和多行注释快捷键 单多行注释就一个组合键:选中+Ctrl+/ 3.Python代码块批量添加Tab缩进 按Ta ...
- github配置单个及多个sshkey的操作方法
一.配置单个sshkey 配置SSHKey命令操作: git --version //检验git是否装好了 cd / cd ~/.ssh //到master目录下 ll //展示所有文件 ssh-ke ...
- webpack使用中遇到的相关问题
问题一:使用webpack打包jquery后,在页面使用错误信息:$ is not defined new webpack.ProvidePlugin({ "$": "j ...