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 ...
随机推荐
- jQuery hooks源码学习
段落不够清晰,待整理 看jQuery源码的时候,经常见到含有hooks标志的对象,如cssHooks, attrHooks, propHooks, valHooks. 下面对其中的一段进行解读. jQ ...
- All Metro Apps on Windows 8.1 Do Not Work
所有的Metro Apps不能够正常打开,表现为打开后自动最小化到任务栏,并且不能恢复正常状态.在Event Viewer\Application中相应的错误信息为: Activation of ap ...
- redis启动加载过程、数据持久化
背景 公司一年的部分业务数据放在redis服务器上,但数据量比较大,单纯的string类型数据一年就将近32G,而且是经过压缩后的. 所以我在想能否通过获取string数据的时间改为保存list数据类 ...
- angular中ng-bind指令小案例
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- java学习笔记4——返回值
这个简单,返回值就是计算结果. 打个比方:个表格中我只要结果,不要经过,这个返回值就是结果.这个过程就是函数. 另外还有一个函数套用一个函数,被套用的函数的结果作为一个返回值给套用的外层函使用.比如: ...
- 关于table排版
colspan和rowspan这两个属性用于创建特殊的表格. colspan是“column span(跨列)”的缩写.colspan属性用在td标签中,用来指定单元格横向跨越的列数: 在浏览器中 ...
- Java中数组的概念与特点
数组概念: 数组其实也是一个容器,可以用来存储固定个数相同类型的数据数组的定义 数组中存储的数据叫做元素 特点: 1.数组是引用数据类型 2.数组的长度是固定的,也就是说可以存储固定个数的数据 3.数 ...
- VS Code编写html(2)
<html> <head> <title>welcome to myspace</title> <meta charset="utf-8 ...
- Day 14 匿名函数
递归之二分法 def sc_func(num,lis): lis_len=int(len(lis)/2) binary_num=lis[lis_len] # print(binary_num) if ...
- jemeter参数化读取文件
1.新建一个123.csv文件 如图 2.添加http请求 3.添加一个查看结果树 二.通过函数助手 (1)打开函数助手 ......... 在json属组中引入变量 这是别人参考的