【CF609C】 Load Balancing
来源:互联网 发布:菜鸟网络上海总部 编辑:程序博客网 时间:2024/05/21 07:06
Tutorial
Denote s — the sum of elements in array. If s is divisible by n then the balanced array consists of n elements . In this case the difference between maximal and minimal elements is 0. Easy to see that in any other case the answer is greater than 0. On the other hand the array consists of numbers and numbers is balanced with the difference equals to 1. Let's denote this balanced array b. To get array b let's sort array a in nonincreasing order and match element ai to element bi. Now we should increase some elements and decrease others. In one operation we can increase some element and decrease another, so the answer is
第二个例子,之前一直想的是l=10,r=11,碰到比l小的ans加,碰到比r大的ans减,但是会有问题,因为平衡后值等于r的个数是可以算的。。
#include <stdio.h>#include <algorithm>#include <math.h>using namespace std;int n,a[111000],b[100100],c[100100];int main(){ scanf("%d",&n); int sum=0; for(int i=0; i<n; i++) { scanf("%d",&a[i]); sum+=a[i]; } int p=sum/n,l,r; if(p*n==sum) l=r=p; else { l=p; r=p+1; } sort(a,a+n); int k=sum%n; for(int i=0; i<n; i++) { if(i<n-k) c[i]=l; else c[i]=r; } int ans=0; for(int i=0; i<n; i++) ans+=fabs(a[i]-c[i]); printf("%d\n",ans/2);}
- 【CF609C】 Load Balancing
- Load Balancing
- load balancing
- Load Balancing
- Load Balancing
- Load Balancing Web Applications
- Server Load Balancing
- Apache + Tomcat + Load Balancing
- Load balancing (computing)
- Hbase Load Balancing
- hdu 2850 Load Balancing
- Runtime Connection Load Balancing
- Load Balancing Web Applications
- HDU 2850 Load Balancing
- ICPC 6924 Load Balancing
- Codeforce----Load Balancing
- Nginx Load Balancing
- 【Usaco2016 FEB】Load Balancing
- 单例模式的几种写法(附介绍)
- gdb学习(一)[第二版]
- 能输入的select下拉框
- 【Python开发】【神经网络与深度学习】网络爬虫之图片自动下载器
- Linux软件安装管理
- 【CF609C】 Load Balancing
- 质数因子
- Google C++编程风格指南之代码注释
- Android开发——View滑动冲突解决方案
- JDK API从下载到使用
- 解决java解析XML文件时的“伪属性名称”问题
- 关于底层的思考
- _CRT_SECURE_NO_WARNINGS
- Spark蘑菇云学习之路(项目实战)--1、项目概述