https://oj.turingedu.cn/class/40/lesson/1/problem/2求调
fanchengyou
2023-09-10 10:02:22
#include <iostream>
#include <algorithm>
#include <cmath>
#define ll long long
using namespace std;
const int N = 200000 + 10;
int n, q;
int a[N], sum[N]; // sum[i] 表示a[1] - a[i] 的和(前缀和)
int main() {
cin >> n >> q;
for (int i = 1; i <= n; i++) {
cin >> a[i];
sum[i] = sum[i - 1] + a[i];
}
sort(a + 1, a + n + 1);
for (int i = 1; i <= q; i++) {
int x;
cin >> x;
int pos = lower_bound(a + 1, a + n + 1, x) - a;
ll ans = 0;
ans = (n - pos) * x - sum[pos - 1];// 小于x
ans += (sum[n] - sum[pos - 1]) * x - pos * x; // 大于等于x
cout << ans << endl;
}
return 0;
}
共 1 条回复
我来教你!