题意:
定义 f(l,r)=∑l≤i≤r ai⋅bi ans= ∑1≤l≤r≤n f(l,r)
给出两个相同长度序列 a b 要求调整b序列 使得ans最小
因为a是不变的 先给a乘上对应的系数 然后排序 进行贪心乘即可
被数据范围wa了好久
注意: (运算符优先级)
a[L]*=((ll)k); a[R]*=((ll)k);//正确写法 a[L]*=(ll)k; a[R]*=(ll)k;//错误写法
下次得全开ll比较好
#include<bits/stdc++.h> using namespace std; //input by bxd #define rep(i,a,b) for(int i=(a);i<=(b);i++) #define repp(i,a,b) for(int i=(a);i>=(b);--i) #define RI(n) scanf("%d",&(n)) #define RII(n,m) scanf("%d%d",&n,&m) #define RIII(n,m,k) scanf("%d%d%d",&n,&m,&k) #define RS(s) scanf("%s",s); #define ll long long #define pb push_back #define REP(i,N) for(int i=0;i<(N);i++) #define CLR(A,v) memset(A,v,sizeof A) ////////////////////////////////// #define inf 0x3f3f3f3f const int N=2e6+5; const ll mod=998244353; ll a[N],b[N]; int main() { ll n;cin>>n; rep(i,1,n) scanf("%lld",&a[i]); rep(i,1,n) scanf("%lld",&b[i]); ll k=n,d=n-2; int L=1,R=n; while(L<=R) { if(L!=R) { a[L]*=k; a[R]*=k;//注意这里不能提前mod 会影响答案的正确性 } else a[L]*=k; k+=d;d-=2;L++,R--; } sort(a+1,a+1+n); sort(b+1,b+1+n); ll ans=0; rep(i,1,n) ans=(ans+ a[i]%mod*b[n-i+1]%mod )%mod ; cout<<ans; return 0; }
仔细发现系数还有另外一种规律
#include <bits/stdc++.h> using namespace std; int64_t n,i,r,Z=998244353,a[222000],b[222000]; int main(){ cin>>n; for(;i<n+n;i++)cin>>(i<n?a[i]:b[i-n]); for(i=0;i<n;i++)a[i]*=(i+1)*(n-i); sort(a,a+n);sort(b,b+n); for(i=0;i<n;i++)r+=a[i]%Z*b[n-1-i],r%=Z; cout<<r; }