4 条题解

  • 2
    @ 2021-8-7 19:00:36

    C++ :

    #include<bits/stdc++.h>
    using namespace std;
    int n,a[1500000],ave;
    long long c[1850000],tot;
    int main()
    {
        scanf("%d",&n);
        for(int i=1;i<=n;i++){
            scanf("%d",&a[i]);
            tot+=a[i];
        }
        ave=tot/n;
        for(int i=1;i<=n-1;i++)
            c[i]=c[i-1]+a[i]-ave;
            
        sort(c,c+n);
        int mid=(n-1)/2;
        long long ans=0;
        for(int i=0;i<=mid;++i)
            ans+=c[n-i-1]-c[i];
    
        printf("%lld",ans);
    }
    
    • 0
      @ 2025-3-30 10:43:19

      #include<bits/stdc++.h> using namespace std; int n,a[1500000],ave; long long c[1850000],tot; int main() { scanf("%d",&n); for(int i=1;i<=n;i++){ scanf("%d",&a[i]); tot+=a[i]; } ave=tot/n; for(int i=1;i<=n-1;i++) c[i]=c[i-1]+a[i]-ave;

      sort(c,c+n);
      int mid=(n-1)/2;
      long long ans=0;
      for(int i=0;i<=mid;++i)
          ans+=c[n-i-1]-c[i];
      
      printf("%lld",ans);
      

      }

      • 0
        @ 2025-3-5 17:29:59
        #include<bits/stdc++.h>
        typedef long long LL;
        const int INF = 0x3f3f3f3f;
        const int N = 1e6+5;
        LL n,a[N],b[N],x,s,ans;
        using namespace std;
        int main()
        {
        	cin>>n;
        	for(int i=1;i<=n;++i){
        		cin>>a[i];
        		x+=a[i];
        	}
        	x/=n;
        	for(int i=1;i<=n;++i)b[i]=b[i-1]+x-a[i-1];
        	sort(b+1,b+n+1);
        	s=b[(n+1)/2];
        	for(int i=1;i<=n;++i)ans+=abs(s-b[i]);
        	cout<<ans;
        	return 0;
        }
        
        • 0
          @ 2024-10-15 17:36:58
          #include<bits/stdc++.h>
          using namespace std;
          int n,a[1500000],ave;
          long long c[1850000],tot;
          int main()
          {
              scanf("%d",&n);
              for(int i=1;i<=n;i++){
                  scanf("%d",&a[i]);
                  tot+=a[i];
              }
              ave=tot/n;
              for(int i=1;i<=n-1;i++)
                  c[i]=c[i-1]+a[i]-ave;
                  
              sort(c,c+n);
              int mid=(n-1)/2;
              long long ans=0;
              for(int i=0;i<=mid;++i)
                  ans+=c[n-i-1]-c[i];
          
              printf("%lld",ans);
          }
          
          
          • 1

          信息

          ID
          33
          时间
          1000ms
          内存
          128MiB
          难度
          2
          标签
          递交数
          157
          已通过
          92
          上传者