[ABC321D] Set Menu 二分 + 前缀和
作者:
多米尼克領主的致意
,
2024-05-16 14:35:44
,
所有人可见
,
阅读 3
注意a[i] + b[1]大于p和a[i] + b[m]小于p两种情况
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
typedef long long ll;
ll n, m, p;
ll a[N], b[N];
ll s[N];
ll ans;
bool check(int x, ll a){
if(b[x] + a < p)return false;
return true;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> m >> p;
for(int i = 1;i <= n;i++)cin >> a[i];
for(int j = 1;j <= m;j++)cin >> b[j];
sort(b + 1, b + 1 + m);
for(int i = 1;i <= m;i++)s[i] = s[i - 1] + b[i];
for(int i = 1;i <= n;i++){
if(a[i] + b[m] < p){
ans += m * a[i] + s[m];
continue;
}
ll l = 1, r = m;
while(l < r){
int mid = (l + r) >> 1;
if(check(mid, a[i]))r = mid;
else l = mid + 1;
}
// cout << l << " ";
ans += (l - 1) * a[i] + s[l - 1];
ans += (m - l + 1) * p;
// cout << ans << endl;
}
cout << ans;
return 0;
}