codeforces 440B. Balancer 解题报告
题目链接:http://codeforces.com/problemset/problem/440/B
题目意思:给出 n 个数,求出这 n 个数的平均值avg,问对于这 n 个数里面中的每一个数,要变为avg 至少需要向相邻的数交换多少次。每次交换只能向相邻的数取1(+1 或者 -1)
一开始想错了,以为每个数直接跟avg比较,求出它们的差值就是答案。后来关电脑在床上的时候就想到了正确的解法。因为考虑到今天有正式的比赛,所以不像那天又继续奋斗了(累~~~),早上一早验证,果然是对的^_^
正确的解法是:与avg比较之后,这会影响到旁边的数!!!可以按着某一个顺序来处理,比如从左到右。那么假如处理到第 i 位的数ai,如果它比avg要小,由于最终就是要令ai变成avg,那么求出它跟avg的差cha = avg-ai,注意:这个差是来源于它右边的数a[i+1]的!因此a[i+1]需要减去这个cha,即a[i+1] -= cha。如果ai比avg要大,则cha = ai-avg,a[i+1] += cha。而统计次数是通过不断将 cha 相加来求得的。
个人感觉像模拟多过像贪心咯~~~
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
using namespace std; #define LL long long
const int maxn = + ;
LL a[maxn];
LL sum; int main()
{
int n;
while (scanf("%d", &n) != EOF)
{
sum = ;
for (int i = ; i <= n; i++)
{
scanf("%lld", &a[i]);
sum += a[i];
}
LL avg = sum / n;
LL cnt = ; for (int i = ; i < n; i++)
{
if (a[i] <= avg)
{
cnt += avg-a[i];
a[i+] -= (avg-a[i]);
}
else
{
cnt += a[i]-avg;
a[i+] += (a[i]-avg);
}
}
printf("%lld\n", cnt);
}
return ;
}
codeforces 440B. Balancer 解题报告的更多相关文章
- codeforces 31C Schedule 解题报告
题目链接:http://codeforces.com/problemset/problem/31/C 题目意思:给出 n 个 lessons 你,每个lesson 有对应的 起始和结束时间.问通过删除 ...
- codeforces 499B.Lecture 解题报告
题目链接:http://codeforces.com/problemset/problem/499/B 题目意思:给出两种语言下 m 个单词表(word1, word2)的一一对应,以及 profes ...
- codeforces 495C. Treasure 解题报告
题目链接:http://codeforces.com/problemset/problem/495/C 题目意思:给出一串只有三种字符( ')','(' 和 '#')组成的字符串,每个位置的这个字符 ...
- codeforces 490B.Queue 解题报告
题目链接:http://codeforces.com/problemset/problem/490/B 题目意思:给出每个人 i 站在他前面的人的编号 ai 和后面的人的编号 bi.注意,排在第一个位 ...
- CodeForces 166E -Tetrahedron解题报告
这是本人写的第一次博客,学了半年的基础C语言,初学算法,若有错误还请指正. 题目链接:http://codeforces.com/contest/166/problem/E E. Tetrahedro ...
- codeforces 489A.SwapSort 解题报告
题目链接:http://codeforces.com/problemset/problem/489/A 题目意思:给出一个 n 个无序的序列,问能通过两两交换,需要多少次使得整个序列最终呈现非递减形式 ...
- codeforces 485A.Factory 解题报告
题目链接:http://codeforces.com/problemset/problem/485/A 题目意思:给出 a 和 m,a 表示第一日的details,要求该日结束时要多生产 a mod ...
- codeforces 483A. Counterexample 解题报告
题目链接:http://codeforces.com/problemset/problem/483/A 题目意思:给出一个区间 [l, r],要从中找出a, b, c,需要满足 a, b 互质,b, ...
- codeforces 479C Exams 解题报告
题目链接:http://codeforces.com/problemset/problem/479/C 题目意思:简单来说,就是有个人需要通过 n 门考试,每场考试他可以选择ai, bi 这其中一个时 ...
随机推荐
- angular中的this指向问题
this是指向当前$scope的. 例如在ng-click的使用中,this是指向当前的$scope而并不是dom元素的. 我们可以使用this的一些方法和属性 我们打印一下this就会发现,this ...
- POJ 1182 食物链 [并查集 带权并查集 开拓思路]
传送门 P - 食物链 Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u Submit ...
- spark与Scala安装过程和步骤及sparkshell命令的使用
Spark与Scala版本兼容问题: Spark运行在Java 8 +,Python 2.7 + / 3.4 +和R 3.1+上.对于Scala API,Spark 2.4.2使用Scala 2.12 ...
- 转 vs2010转vs2008 其他的一样
如果你使用VS2010的任何版本写代码,那么在VS2008中就不能打开VS2010的解决方案了,为此,通过以下三步就可以解决了一.对于工程名.sln; 1.用你喜欢的编辑器打开sln文件,比如note ...
- Spring的Bean定义
以下内容引用自http://wiki.jikexueyuan.com/project/spring/bean-definition.html: Bean定义 被称作bean的对象是构成应用程序的支柱也 ...
- 一道题目- Find the smallest range that includes at least one number from each of the k lists
You have k lists of sorted integers. Find the smallest range that includes at least one number from ...
- fatal error C1189: #error : core.hpp header must be compiled as C++
两次opencv工程需要设置为C++编译:找了一半天的解决方法. I am building a C application that uses OpenCV. when compiling, I g ...
- SpringCloud中Rabbitmq的使用
1.pom配置,添加以来jar包 <dependency> <groupId>org.springframework.cloud</groupId> <art ...
- [AngularJS + Unit Testing] Testing a component with requiring ngModel
The component test: describe('The component test', () => { let component, $componentController, $ ...
- linux 进程间通信之 消息队列
消息队列就是一个消息的链表. 能够把消息看作一个记录,具有特定的格式以及特定的优先级.对消息队列有写权限的进程能够向中依照一定的规则加入新消息.有读权限的进程则能够读走消息. 读走就没有了.消息队列是 ...