yezifenghua_coding @ 2024-08-21 22:31:40
#include <iostream>
#include <cmath>
#include <algorithm>
#define int long long
using namespace std;
const int M = 1e5;
int qm[M];
int m, n;
signed main()
{
scanf("%d %d", &m, &n);
for (int i = 0; i < m; i++)
{
scanf("%d", &qm[i]);
}
sort(qm, qm + m);
int total = 0;
int x;
while (n--)
{
scanf("%d", &x);
int l = 0, r = m - 1;
while (l != r - 1)
{
int mid = l + r >> 1;
if (qm[mid] >= x)
r = mid;
else
l = mid + 1;
}
int a = abs(qm[l] - x);
int b = abs(qm[r] - x);
total += (a < b) ? a : b;
}
cout << total << endl;
return 0;
}
by haimingbei @ 2024-08-21 22:45:58
@yezifenghua_coding 懒得看,其实用lower_bound()它不香吗? (求关注)
/*
2、P1678 烦恼的高考志愿
*/
#include<bits/stdc++.h>
using namespace std;
int main(){
long long m,n,school[100010]={0},ans=0;
cin>>m>>n;
for(int i=1;i<=m;i++)cin>>school[i];
int b;
sort(school+1,school+m+1);
for(int i=1;i<=n;i++){
cin>>b;
int x=lower_bound(school+1,school+m+1,b)-school;
if(x==m+1)ans+=b-school[m];
else if(x==1)ans+=school[1]-b;
else ans+=min(abs(b-school[x-1]),abs(b-school[x]));
}
cout<<ans;
return 0;
}
by __youzimo2014__ @ 2024-11-30 12:12:19
@haimingbei 不是哥们你直接“懒得看”然后贴个代码,然后就“求关注”了?真6