Two Arrays and Sum of Functions CodeForces - 1165E
原题链接
考察:贪心+思维
思路:
易知 反序和<=乱序和<=正序和.但是这道题是求\(f[l,r]\)的累加和.我们可以发现由于a是固定的,每个\(a[i]\)对res的贡献次数也是固定的:即\(i*(n-i+1)\),也就是求\(\sum_1^n a[i]*b[i]*i*(n-i+1)\),因为对于\(a[i]\)来说,它的顺序在数组已定,那么求和的\(a[i]*i*(n-i+1)\)也是固定的,我们将\(a[i]*i*(n-i+1)\)排序求反序和即可.
Code
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
typedef long long LL;
const int N = 200010,M = 998244353;
int n,b[N];
LL a[N];
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++) scanf("%lld",&a[i]),a[i] = a[i]*i*(n-i+1);
sort(a+1,a+n+1);
for(int i=1;i<=n;i++) scanf("%d",&b[i]);
sort(b+1,b+n+1);
LL res = 0;
for(int i=1;i<=n;i++)
res = (res+a[i]%M*b[n-i+1]%M)%M;
printf("%lld\n",res);
return 0;
}