首页 > 其他 > 详细

cf1283D——bfs

时间:2020-01-06 22:50:05      阅读:79      评论:0      收藏:0      [点我收藏+]

很简单的题

#include<bits/stdc++.h>
using namespace std;
#define N 200005
#define ll long long 
ll x[N],n,m;

map<ll,int>mp;
vector<ll>ans;
struct Node{
    ll pos,ori;
    Node(){}
    Node(ll pos,ll ori):pos(pos),ori(ori){}
};
queue<Node>q;

int main(){
    cin>>n>>m;
    for(int i=1;i<=n;i++){
        cin>>x[i];
        mp[x[i]]=1;
        q.push(Node(x[i]-1,x[i]));
        q.push(Node(x[i]+1,x[i]));
    }
    
    ll sum=0;
    while(m){
        Node now=q.front();q.pop();
        if(mp[now.pos])continue;
        m--;
        mp[now.pos]=1;
        sum+=abs(now.ori-now.pos);
        ans.push_back(now.pos);
        q.push(Node(now.pos-1,now.ori));
        q.push(Node(now.pos+1,now.ori));
    }
    cout<<sum<<endl;
    for(auto x:ans)cout<<x<<" ";
    
} 

cf1283D——bfs

原文:https://www.cnblogs.com/zsben991126/p/12158443.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!