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;
}
全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务