点击打开链接uva 11300
思路:数学分析+贪心
分析:
1 首先最终每个人的金币数量可以计算出来,它等于金币总数除以人数n。接下来我们用m来表示每人的最终的金币数
2 现在假设编号为i的人初始化为Ai枚金币,Xi表示第i个人给第i-1个人Xi枚金币,对于第一个人来说他是给第n个人。
3 根据第二点可以知道 则C0 = 0
对于第一个人 A1-X1+X2 = m X2 = m-A1+X1 = X1-C1(规定C1 = A1-m,一下类似) 则C1 = C0+A1-m
对于第二个人 A2-X2+X3 = m X3 = m-A2+X2 = 2m-A1-A2+X1 = X1-C2 则C2 = -2m+A1+A2 = C1+A2-m
...
对于第n个人 An-Xn+X1 = m Xn = m-An+Xn = X1-Cn 则Cn = Cn-1+An-m
那么根据题目ans = X1+X2+X3+...+Xn = X1+X1-C1+X1-C2+...+Xn-Cn。那么如果ans要最小,根据数轴X1-Cn表示的是点X1到点Cn的距离,那么ans要最小就是使得X1为这些点的“中位数”,我们只要找到C数组并且找到中位数即可。
代码:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long int64;
const int MAXN = 1000010;
int64 C[MAXN] , A[MAXN];
int main(){
int n;
while(scanf("%d" , &n) != EOF){
int64 sum;
sum = 0;
for(int i = 0 ; i < n ; i++){
scanf("%lld" , &A[i]);
sum += A[i];
}
int64 m = sum/n;//求出每个人最后的金币数
C[0] = 0;//C[0]为0
for(int i = 1 ; i < n ; i++)
C[i] = C[i-1]+A[i]-m;//求C数组
sort(C , C+n);
int64 mid;
if(n%2 == 0)//偶数
mid = (C[n/2]+C[n/2-1])>>1;
else
mid = C[n/2];
int64 ans = 0;
for(int i = 0 ; i < n ; i++)
ans += abs(mid-C[i]);
printf("%lld\n" , ans);
}
return 0;
}
分享到:
相关推荐
Novel iterative equalization based on energy-spreading transform
The BER performance of the coherent time-spreading OCDMA network is analyzed by considering the MAI and beat noises as well as the other additive noises. The influence and solution for the beat noise ...
The performance of normalized throughput in wavelength division multiplexing (WDM) based coherent time-spreading optical code division multiple access (TS-OCDMA) system is studied. The upper bound and...
矩阵用matlab代码实现
扩散激活 使用在pyspark和graphframe中创建的使用扩散激活的扩散模型。
direct sequence spread spectrum matlab program to achieve the spreading of original data
Spreading the Wealth and the Growth Dilemma The Logic of Uncontrolled Growth How Much Consumption? Making Space for a New Set of Voices Solving the Growth Dilemma Notes 11 Putting Poverty in ...
In this paper we propose a Swarm-based Spreading Points algorithm (SSP) for improving the solutions for packing problems. The SSP repositions the initial set of points and evolves it to improve the ...
Advanced particle effects like global/local Turbulence & Attractors, Particle sticking- propagation-spreading & particle to particle collisions (ideal for ice/fire simulation), Tornadoes, Volumetric ...
A virus is spreading rapidly, and your task is to quarantine the infected area by installing walls. The world is modeled as a 2-D array of cells, where 0 represents uninfected cells, and 1 represents...
A virus is spreading rapidly, and your task is to quarantine the infected area by installing walls. The world is modeled as a 2-D array of cells, where 0 represents uninfected cells, and 1 represents ...
1-4 Describe the spread mechanism used in UMTS, and please draw the power spectrum before and after spreading. 1-5 Please describe the functionalities of rake receiver? 2-1 Explain (draw) the 4 TCP-IP...
对于论文Phase transitions in information spreading on structured populations的翻译
Ultra Wide Band (UWB) is a technology for the transmission of data using techniques which cause a spreading of the radio energy over a very wide frequency band, with a very low power spectral density....
《黑暗即蔓延》是Flood-it类型游戏的翻版。 这是一个用PHP编写的开源免费游戏。 它应该托管在能够处理PHP的服务器上,并且很容易添加级别。
The OS SIS ICD Draft 1 provides an overview of the Galileo system, the signal-in-space radio frequency characteristics, the characteristics of the spreading codes, the message structures and the ...
Cortical spreading depression (CSD), which is a significant pathological phenomenon that correlates with migraines and cerebral ischemia, has been characterized by a wave of depolarization among ...
The problem with these traditional marketing techniques is that they have become less effective at spreading the word as people get better at blocking out these interruptions. Ten years ago, buying a...
均匀与非均匀网络上的疾病传播动力学建模,胡瑶,闵乐泉,本文提出了基于均匀网络和不均匀网络的SIRS模型. 在均匀网络上, 本文证明了如果基本复制数$R_0$的值小于1, 则模型的无病平衡点是局部�
伊利诺伊大学香槟分校的一篇博士学位论文,详尽分析了并行捕获方法的原理,希望对初学伪码捕获的同学有帮助。