D. Christmas Trees

链接:https://codeforces.com/contest/1283/problem/D

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

Copy

2 6
1 5

output

Copy

8
-1 2 6 4 0 3 

input

Copy

3 5
0 3 1

output

Copy

7
5 -2 4 -1 2 

代码:

#include <bits/stdc++.h>
using namespace std;
long long t,n,a,b,k,s=0,sum=0;
queue<long long>q;
long long y[200005];
map<long long ,long long>m,l;
int main()
{
    scanf("%lld %lld",&n,&k);
    for(int i=1;i<=n;i++)
    {
    	scanf("%lld",&a);
    	m[a]=1;
    	l[a]=1;
    	q.push(a);
    }
    while(sum<k)
    {
    	a=q.front();
    	q.pop();
    	if(m[a+1]==0)
    	{
    		m[a+1]=1;
    		sum++;
    		y[sum]=a+1;
    		s+=l[a];
    		q.push(a+1);
    		l[a+1]=l[a]+1;
    	}
    	if(sum==k)
    	break;
    	if(m[a-1]==0)
    	{
    		m[a-1]=1;
    		sum++;
    		y[sum]=a-1;
    		s+=l[a];
    		q.push(a-1);
    		l[a-1]=l[a]+1;
    	}
    }
    printf("%lld\n",s);
    for(int i=1;i<=k;i++)
    printf("%lld ",y[i]);
}

 

全部评论

相关推荐

xiaolihuam...:当然还有一种情况是你多次一面挂,并且挂的原因都比较类似,例如每次都是算法题写不出来。面试官给你的评价大概率是算法能力有待加强,算法能力有待提高,基础知识掌握的不错,项目过关,但是coding要加强。短期内高强度面试并且每次都是因为同样的原因挂(这个你自己肯定很清楚),会形成刻板印象,因为你偶尔一次算法写不出来,面试官自己也能理解,因为他清楚的知道自己出去面试也不一定每一次面试算法都能写出来。但是连续几次他发现你的面屏里面都是算法有问题,他就认为这不是运气问题,而是能力问题,这种就是很客观的评价形成了刻白印象,所以你要保证自己。至少不能连续几次面试犯同样的错。算法这个东西比较难保证,但是有些东西是可以的,例如某一轮你挂的时候是因为数据库的索引,这个知识点答的不好,那你就要把数据库整体系统性的复习,下一轮面试你可以,项目打的不好,可以消息队列答的不好,但是绝对不可以数据库再答的不好了。当然事实上对于任何面试都应该这样查漏补缺,只是对于字节来说这个格外重要,有些面试官真的会问之前面试官问过的问题
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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