题解:P15127 [ROIR 2026] 比赛结果

题目传送门

思路

暴力

枚举所有 \(i\),\(j\),判断 \(a_i>a_j\) 后累加差值。

for(i,0,n-1)
    for(j,0,n-1)
        if(a[i]>a[j])ans+=a[i]-a[j];

显然 \(O\left(n^2\right)\) 过不去。

数学优化

我们可以将总和拆分为两部分计算:$ {\textstyle \sum_{a_i>a_j}^{}} \left(a_i-a_j\right)= {\textstyle \sum_{a_i>a_j}^{}}a_i- {\textstyle \sum_{a_i>a_j}^{}}a_j$

对数组进行升序排序后,设排序后的数组为 \(b_0,b_1,\dots,b_{n-1}\),对于第 \(k\) 个元素 \(b_k\):

  • 比前 \(k\) 个元素(\(b_0,\dots,b_{k-1}\))都大,因此会被计入前半部分 \(k\) 次,贡献为 \(b_k\times k\);
  • 前 \(k\) 个元素会被计入后半部分各一次,贡献为 \(-\left(b_0+b_1+\dots+b_{k-1}\right)\)。

所以,每个元素 \(b_k\) 的总贡献就为:\(b_k\times k-\text{前缀和}\left(0∼k-1\right)\)。

所有的贡献之和就是答案。

时间复杂度为 \(O\left(n\log n\right)\)。

注意

不开 long long 见祖宗。

代码

#include<bits/stdc++.h>
#define lcm(x,y) x/__gcd(x,y)*y
#define lb(x) (x&-x)
#define str to_string
#define endl '\n'
#define endr '\r'
#define emp_ '\0'
using namespace std;
#define int long long
using ll=long long;
namespace Fast{
    namespace Fread{const int SZ=1<<20;char buf[SZ],*S,*TT;inline char getchar(){if(S==TT){TT=(S=buf)+fread(buf,1,SZ,stdin);if(S==TT)return endl;}return *S++;}}
    namespace Fwrite{const int SZ=1<<20;char buf[SZ],*S=buf,*TT=buf+SZ;inline void flush(){fwrite(buf,1,S-buf,stdout);S=buf;}inline void putchar(char c){*S++=c;if(S==TT)flush();}struct NTR{~NTR(){flush();}} ztr;}
    using namespace Fread;
    using namespace Fwrite;
    #define getchar Fread::getchar
    #define putchar Fwrite::putchar
    namespace FastIO{
        struct R{
            template<typename T>
            R&operator>>(T&from){from=0;short f=1;char c=getchar();while(c<'0'||c>'9'){if(c=='-')f*=-1;c=getchar();}while(c>='0'&&c<='9')from=(from<<3)+(from<<1)+(c^48),c=getchar();from*=f;return*this;}
            R&operator>>(double&from){from=0;double t=0;short f=1,s=0;char c=getchar();while((c<'0'||c>'9')&&c!='.'){if(c=='-')f*=-1;c=getchar();}while(c>='0'&&c<='9'&&c!='.')from=from*10+(c^48),c=getchar();if(c=='.')c=getchar();else{from*=f;return*this;}while(c>='0'&&c<='9')t=t*10+(c^48),s++,c=getchar();while(s--)t/=10.0;from=(from+t)*f;return*this;}
            R&operator>>(long double&from){from=0;long double t=0;short f=1,s=0;char c=getchar();while((c<'0'||c>'9')&&c!='.'){if(c=='-')f*=-1;c=getchar();}while(c>='0'&&c<='9'&&c!='.')from=from*10+(c^48),c=getchar();if(c=='.')c=getchar();else{from*=f;return*this;}while(c>='0'&&c<='9')t=t*10+(c^48),s++,c=getchar();while(s--)t/=10.0;from=(from+t)*f;return*this;}
            R&operator>>(__float128&from){from=0;__float128 t=0;short f=1,s=0;char c=getchar();while((c<'0'||c>'9')&&c!='.'){if(c=='-')f*=-1;c=getchar();}while(c>='0'&&c<='9'&&c!='.')from=from*10+(c^48),c=getchar();if(c=='.')c=getchar();else{from*=f;return*this;}while(c>='0'&&c<='9')t=t*10+(c^48),s++,c=getchar();while(s--)t/=10.0;from=(from+t)*f;return*this;}
            R&operator>>(char&c){c=getchar();while(c==' '||c==endl||c==endr)c=getchar();return*this;}
            R&operator>>(char*str){int len=0;char c=getchar();while(c==' '||c==endl||c==endr)c=getchar();while(c!=' '&&c!=endl&&c!=endr)str[len++]=c,c=getchar();str[len]=emp_;return*this;}
            R(){}
        }cin;
        struct W{
            const int S=6;
            typedef int M;
            template<typename T>
            W&operator<<(T from){if(from==0){putchar('0');return*this;}if(from<0)putchar('-'),from=-from;static short sta[40];short top=0;while(from>0)sta[++top]=from%10,from/=10;while(top>0)putchar(sta[top]+'0'),top--;return*this;}
            W&operator<<(double from){if(from<0)putchar('-'),from=-from;M _=from;from-=(double)_;static short sta[40];short top=0;while(_>0)sta[++top]=_%10,_/=10;if(top==0)putchar('0');while(top>0)putchar(sta[top]+'0'),top--;putchar('.');for(int i=0;i<S;i++)from*=10;_=from;while(_>0)sta[++top]=_%10,_/=10;for(int i=0;i<S-top;i++)putchar('0');while(top>0)putchar(sta[top]+'0'),top--;return*this;}
            W&operator<<(long double from){if(from<0)putchar('-'),from=-from;M _=from;from-=(long double)_;static short sta[40];short top=0;while(_>0)sta[++top]=_%10,_/=10;if(top==0)putchar('0');while(top>0)putchar(sta[top]+'0'),top--;putchar('.');for(int i=0;i<S;i++)from*=10;_=from;while(_>0)sta[++top]=_%10,_/=10;for(int i=0;i<S-top;i++)putchar('0');while(top>0)putchar(sta[top]+'0'),top--;return*this;}
            W&operator<<(__float128 from){if(from<0)putchar('-'),from=-from;M _=from;from-=(__float128)_;static short sta[40];short top=0;while(_>0)sta[++top]=_%10,_/=10;if(top==0)putchar('0');while(top>0)putchar(sta[top]+'0'),top--;putchar('.');for(int i=0;i<S;i++)from*=10;_=from;while(_>0)sta[++top]=_%10,_/=10;for(int i=0;i<S-top;i++)putchar('0');while(top>0)putchar(sta[top]+'0'),top--;return*this;}
            W&operator<<(char c){putchar(c);return*this;}
            W&operator<<(char*str){int cur=0;while(str[cur])putchar(str[cur++]);return*this;}
            W&operator<<(const char*str){int cur=0;while(str[cur])putchar(str[cur++]);return*this;}
            W(){}
        }cout;
    }
    int max(register const int e,register const int b){register const int from(e),to(b);return(from>to?from:to);}
    int min(register const int e,register const int b){register const int from(e),to(b);return(from>to?to:from);}
}
using namespace Fast;
#define cin FastIO::cin
#define cout FastIO::cout
const double EPS=1e-6,PAI=acos(-1.0);
const int MAX=5e5+5,mod=1e9+7,MOD=998244353;
int n;
void solve(){
    cin>>n;
    vector<int>a(n);
    for(int i=0;i<n;i++)cin>>a[i];
    sort(a.begin(),a.end()); // 排序
    int ans=0,pre=0; // 前缀和
    for(int k=0;k<n;k++){
        ans+=a[k]*k-pre; // 当前元素贡献 =a_k*k- 前面k个元素之和
        pre+=a[k]; // 更新前缀和
    }
    cout<<ans<<endl;
}
signed main(){
    int t=1;
//  cin>>t;
    while(t--)solve();
    return 0;
}

AC 记录

posted @ 2026-11-23 01:26  阿蕾奇诺Arlecchino  阅读(9)  评论(0)    收藏  举报