CodeForces - 1165E Two Arrays and Sum of Functions(贪心)

题目链接

题目大意

  给你两个数组,让你对b数组排序,使得所给公式算出的结果最小。

解题思路

  思路很显然,算出来每个位置会被算上的次数然后直接乘到a上再对a排序就行了,但是有个特别坑的点就是需要先排序后取模,否则原来的大小关系就会变化。

代码

const int maxn = 3e5+10;
const int maxm = 1e6+10;

ll a[maxn], b[maxn], times[maxn];
int n;
int main() {
	cin >> n;
	for (int i = 1; i<=n; ++i) cin >> a[i];
	for (int i = 1; i<=n; ++i) cin >> b[i];
	for (int i = 1; i<=n; ++i) times[i] = 1LL*i*(n-i+1);
	for (int i = 1; i<=n; ++i) a[i] = a[i]*times[i];
	//for (int i = 1; i<=n; ++i) printf(i==n ? "%d\n":"%d ", times[i]);
	sort(a+1, a+n+1);
	sort(b+1, b+n+1, greater<ll>());
	ll ans = 0;
	for (int i = 1; i<=n; ++i) ans = (ans+a[i]%MOD*b[i]%MOD)%MOD;
	cout << ans << endl;
	return 0;
}
上一篇:2021-02-22


下一篇:Azure Functions Error Value cannot be null. (Parameter 'provider')