卡夫卡 @ 2021-08-05 12:04:34
将
while(abs(x-b[k+1])<t)
中的<改为<=就AC了,为什么?
#include <iostream>
#include <cstdio>
#include "algorithm"
#include "queue"
#include "cmath"
#define ll long long
using namespace std;
const int maxn=1e5+5;
priority_queue<int,vector<int>,greater<int> >q;
int n,m,t;
int b[maxn];
ll sum;
int main() {
scanf("%d%d",&m,&n);
for(int i=1;i<=n;i++)
scanf("%d",&b[i]);//学校成绩
for(int i=1;i<=m;i++){
scanf("%d",&t);//学生成绩
q.push(t);
}
sort(b+1,b+1+n);
int k=1;
for(int i=1;i<=n;i++){
int x=q.top(),t=abs(x-b[k]);
q.pop();//忘记了qwq
while(abs(x-b[k+1])<t){//为什么要<=?
k++;
t=abs(x-b[k]);
}
sum+=t;
}
printf("%lld\n",sum);
return 0;
}
求dalao解惑