Christmas Trees(bfs)

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)/~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

starlet_kiss

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值