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 ...
随机推荐
- 9.14[XJOI] NOIP训练33
今日9.14 洛谷打卡:大凶!!!(换个字体玩玩qwq) -------------------------------------------------------- 一个超颓的上午 今天又是fl ...
- css 中font属性知识点总结
一. font属性值可以继承.例如子元素可以继承父元素的行高,字体大小等等. 二.font属性可以进行连写:font: font-sytle font-weight font-size/line- ...
- 在redhat6.4上编译z3求解器
因为项目需要,我们使用到了微软的z3求解器求约束,但是z3求解器在红帽平台上并没有发布编译好的二进制版本,而我们的运行环境是红帽的企业版6.4,因此需要自己编译相应的二进制. z3是由微软公司开发的一 ...
- 关于电脑安装新硬盘,出现无法是识别设备,03F0问题解答。
问题说明:在添加新的硬盘,切确定硬盘没有坏的情况下,无法识别出新的硬盘. 解决方案: 1.检查bios系统里的安全模式,是否处于开启中.因为在windows 8.1以上的版本中,不开启的情况下只能读取 ...
- bzoj 1191: [HNOI2006]超级英雄Hero 网络流_残量网络
题目描述: 现在电视台有一种节目叫做超级英雄,大概的流程就是每位选手到台上回答主持人的几个问题,然后根据回答问题的 多少获得不同数目的奖品或奖金.主持人问题准备了若干道题目,只有当选手正确回答一道题后 ...
- python与图灵机器人交互(ITCHAT版本)
#!/usr/bin/env python#-*- coding:utf-8 -*- @Author : wujf @Time:2018/9/5 17:42import requestsimport ...
- html中的meta标签属性
<!-- 声明文档使用的字符编码 --> <meta charset='utf-8'> <!-- 优先使用 IE 最新版本和 Chrome --> <meta ...
- P2024 食物链 (补集)
题目描述 动物王国中有三类动物 A,B,C,这三类动物的食物链构成了有趣的环形.A 吃 B,B 吃 C,C 吃 A. 现有 N 个动物,以 1 - N 编号.每个动物都是 A,B,C 中的一种,但是我 ...
- adb简单使用
一.基本命令 1.查看目前连接的设备/虚拟器的状态 adb devices
- 02018_StringBuffer练习
1.已知int[] arr = {34,12,89,68}; 将其中的元素转成字符串,格式 [34,12,89,68]: 参考:02011_定义打印数组元素方法,按照给定的格式打印[11, 33, 4 ...