很简单的题
#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<<" "; }
原文:https://www.cnblogs.com/zsben991126/p/12158443.html