题面

BZOJ
洛谷

题解

和上一题一样啊。

#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
#define ll long long
#define MAX 100010
inline int read()
{
    int x=0,t=1;char ch=getchar();
    while((ch<'0'||ch>'9')&&ch!='-')ch=getchar();
    if(ch=='-')t=-1,ch=getchar();
    while(ch<='9'&&ch>='0')x=x*10+ch-48,ch=getchar();
    return x*t;
}
int n,a[MAX];
ll s[MAX];
int main()
{
	n=read();ll sum=0,ans=0;
	for(int i=1;i<=n;++i)sum+=(a[i]=read());sum/=n;
	for(int i=1;i<=n;++i)s[i]=s[i-1]-a[i]+sum;
	sort(&s[1],&s[n+1]);
	for(int i=1;i<=n;++i)ans+=abs(s[i]-s[(n+1)/2]);
	printf("%lld\n",ans);
	return 0;
}

相关文章:

  • 2022-12-23
  • 2021-04-25
  • 2022-01-09
  • 2022-12-23
  • 2022-01-13
  • 2022-01-16
  • 2022-03-09
  • 2022-12-23
猜你喜欢
  • 2021-12-30
  • 2022-02-15
  • 2021-12-02
  • 2021-10-10
  • 2022-12-23
  • 2021-09-23
  • 2021-09-26
相关资源
相似解决方案