codeforces 1283D
题意:
题意:现在已知圣诞树在x位置,求人的位置,使每个人到达距离他最近的树的距离之和最小
安排人肯定从树旁边开始安排,树的距离为1的位置安排满之后继续安排下一层,因为线无线长,也没什么限制,就直接bfs
#include <bits/stdc++.h>
using namespace std;
map<int,bool> vis;
queue<pair<int,int>> q;
vector<int> v;
int main(){
int n,m;
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++){
int x;
scanf("%d",&x);
vis[x] = 1;
q.push({1,x-1});q.push({1,x+1});
}
long long ans = 0;
vector<int> v;
while(!q.empty() && m>0){
auto t = q.front();q.pop();
if(!vis[t.second]){
ans+=t.first; m--;
v.push_back(t.second);
vis[t.second] = 1;
q.push({t.first+1,t.second-1});
q.push({t.first+1,t.second+1});
}
}
printf("%lld\n",ans);
for(int t:v){
cout<<t<<" ";
}
cout<<endl;
return 0;
}
查看26道真题和解析