There are nn Christmas trees on an infinite number line. The ii-th tree grows at the position xixi. All xixi are guaranteed to be distinct.
Each integer point can be either occupied by the Christmas tree, by the human or not occupied at all. Non-integer points cannot be occupied by anything.
There are mm people who want to celebrate Christmas. Let y1,y2,…,ymy1,y2,…,ym be the positions of people (note that all values x1,x2,…,xn,y1,y2,…,ymx1,x2,…,xn,y1,y2,…,ym should be distinct and all yjyj should be integer). You want to find such an arrangement of people that the value ∑j=1mmini=1n|xi−yj|∑j=1mmini=1n|xi−yj| is the minimum possible (in other words, the sum of distances to the nearest Christmas tree for all people should be minimized).
In other words, let djdj be the distance from the jj-th human to the nearest Christmas tree (dj=mini=1n|yj−xi|dj=mini=1n|yj−xi|). Then you need to choose such positions y1,y2,…,ymy1,y2,…,ym that ∑j=1mdj∑j=1mdj is the minimum possible.
Input
The first line of the input contains two integers nn and mm (1≤n,m≤2⋅1051≤n,m≤2⋅105) — the number of Christmas trees and the number of people.
The second line of the input contains nn integers x1,x2,…,xnx1,x2,…,xn (−109≤xi≤109−109≤xi≤109), where xixi is the position of the ii-th Christmas tree. It is guaranteed that all xixi are distinct.
Output
In the first line print one integer resres — the minimum possible value of ∑j=1mmini=1n|xi−yj|∑j=1mmini=1n|xi−yj| (in other words, the sum of distances to the nearest Christmas tree for all people).
In the second line print mm integers y1,y2,…,ymy1,y2,…,ym (−2⋅109≤yj≤2⋅109−2⋅109≤yj≤2⋅109), where yjyj is the position of the jj-th human. All yjyj should be distinct and all values x1,x2,…,xn,y1,y2,…,ymx1,x2,…,xn,y1,y2,…,ym should be distinct.
If there are multiple answers, print any of them.
Examples
Input
2 6
1 5
Output
8
-1 2 6 4 0 3
Input
3 5
0 3 1
Output
7
5 -2 4 -1 2
题意:有n棵圣诞树,每棵圣诞树占据一个位置。然后有m个人,每个人也要占据一个位置,求出这m个人的位置使得每个人到离自己最近的圣诞树的距离和最小,并且打印最小值和这m个人的位置。
思路:每个人肯定去挨着圣诞树找自己的位置,这样的话,我们就bfs去找每个人的 位置。这样的话既保证了圣诞树离自己是最近的,而且还可以求出距离以及每个人所处的位置。
代码如下:
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxx=2e5+100;
struct node{
int pos;
int num;
node(int x,int y)
{
pos=x;num=y;
}
};
int a[maxx];
int ans[maxx];
map<int,bool> mp;
int n,m;
inline void bfs()
{
queue<node> p;
for(int i=1;i<=n;i++) p.push(node(a[i],0));
ll sum=0;
int cnt=0;
while(p.size()&&cnt<m)
{
node q=p.front();
p.pop();
if(q.num) ans[cnt++]=q.pos,sum+=q.num;
if(mp[q.pos+1]==0)
{
mp[q.pos+1]=1;
p.push(node(q.pos+1,q.num+1));
}
if(mp[q.pos-1]==0)
{
mp[q.pos-1]=1;
p.push(node(q.pos-1,q.num+1));
}
}
cout<<sum<<endl;
for(int i=0;i<cnt;i++) cout<<ans[i]<<" ";
cout<<endl;
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
mp[a[i]]=1;
}
bfs();
return 0;
}
努力加油a啊,(o)/~