当前位置:网站首页>Candy delivery (Mathematics)
Candy delivery (Mathematics)
2022-07-06 16:08:00 【AC__ dream】

This problem is essentially a mathematical problem , Presuppose xi In the best case i A child gave it to No i-1 The number of sweets for a child (xi A negative number means the i-1 A child gave the first i A child candy ), A special case : When i by 1 when , Default i-1=n, That is to say x1 It means the first one 1 A child gave it to No n The number of sweets for a child . Then our The result is all xi The sum of the absolute values of ( The number of candy delivered is the final price ), The title has given the number of sweets each child had at the beginning ai, Let the number of sweets each child finally gets be mid, So the first i The final number of sweets for children mid Depends on the number of sweets you have at the beginning ai And he gave No i-1 The number of sweets for a child xi And the first i+1 A child gave him the number of sweets xi+1, That is to say mid=ai-xi+x(i+1), Simplify to x(i+1)-xi=mid-ai, From this formula, we can get
(1) x2-x1=mid-a1
(2) x3-x2=mid-a2
……
(n-1) xn-x(n-1)=mid-a(n-1)
Combining the above formulas, we get
(1)+(2) have to x3-x1=2*mid-(a2+a1)
(1)+(2)+(3) have to x4-x1=3*mid-(a3+a2+a1)
……
(1)+(2)+……+(n-1) have to xn-x1=(n-1)*mid-(a(n-1)+a(n-2)+……+a1)
Move the terms of the merged formula to get :
x2=x1-(a1-mid)
x3=x1-((a2+a1)-2*mid)
x4=x1-((a3+a2+a1)-3*mid)
……
xn=x1-((a(n-1)+……+a2+a1)-(n-1)*mid)
Again because x1=x1-0
Our final result is xi The sum of the absolute values of , It's easy to think of the distance formula when you see the formula after the term shift above , So our result is One point to 0,(a1-mid),((a2+a1)-2*mid),……,((a(n-1)+……+a2+a1)-(n-1)*mid) The minimum value of the sum of the distances of these points , This can be transformed into the problem of warehouse location , Directly take a median for calculation , Here is the code :
#include<cstdio>
#include<iostream>
#include<cstring>
#include<vector>
#include<algorithm>
#include<map>
#include<cmath>
#include<queue>
using namespace std;
const int N=1000003;
long long a[N],s[N];
int main()
{
int n;
cin>>n;
for(int i=1;i<=n;i++)
{
scanf("%lld",&a[i]);
s[i]=s[i-1]+a[i];
}
long long mid=s[n]/n;
for(int i=1;i<=n;i++)
s[i]-=i*mid;
sort(s,s+n);
long long ans=0;
for(int i=0;i<n;i++)
ans+=abs(s[i]-s[n/2]);
printf("%lld",ans);
return 0;
}边栏推荐
- 2027. Minimum number of operations to convert strings
- MySQL grants the user the operation permission of the specified content
- Web based photo digital printing website
- B - Code Party (girls' competition)
- Find 3-friendly Integers
- “鬼鬼祟祟的”新小行星将在本周安全掠过地球:如何观看
- Luogu P1102 A-B number pair (dichotomy, map, double pointer)
- 信息安全-安全专业名称|CVE|RCE|POC|VUL|0DAY
- Essai de pénétration (1) - - outils nécessaires, navigation
- Opencv learning log 32 edge extraction
猜你喜欢

TCP's three handshakes and four waves

Gartner:关于零信任网络访问最佳实践的五个建议

Nodejs+vue online fresh flower shop sales information system express+mysql

X-Forwarded-For详解、如何获取到客户端IP

b站 实时弹幕和历史弹幕 Protobuf 格式解析

PySide6 信号、槽

渗透测试 ( 3 ) --- Metasploit Framework ( MSF )

1010 things that college students majoring in it must do before graduation
Frida hook so layer, protobuf data analysis

树莓派4B安装opencv3.4.0
随机推荐
Data storage in memory & loading into memory to make the program run
1903. Maximum odd number in string
树莓派4B安装opencv3.4.0
Opencv learning log 30 -- histogram equalization
Socket communication
STM32 how to use stlink download program: light LED running light (Library version)
Auto.js入门
想应聘程序员,您的简历就该这样写【精华总结】
最全编程语言在线 API 文档
[teacher Gao UML software modeling foundation] collection of exercises and answers for level 20 cloud class
【练习-6】(PTA)分而治之
JS call camera
Penetration test (4) -- detailed explanation of meterpreter command
Truck History
Opencv learning log 14 - count the number of coins in the picture (regardless of overlap)
Find 3-friendly Integers
C language must memorize code Encyclopedia
【练习-7】(Uva 10976)Fractions Again?!(分数拆分)
快速转 TypeScript 指南
921. Minimum additions to make parentheses valid