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.

一开始理解错题面了,用中位数思路写了一通发现连样例都过不了。这个题的意思是确定n个的位置(人和人,人和树的位置不能有重合),使每个人距离最近的圣诞树的距离的和最小。首先想到把人安排在圣诞树两边(人的位置为树的位置+-1),两边如果被占据的话继续往两边移。因为题目求的是和最小,容易想到bfs的思想(搜到终点能保证最小),所以在此处利用bfs,并利用map来存储当前位置到最近的树的距离。首先把所有树的坐标扔进队列。从队列取数后,先判断这个位置是否为空位置:if(d[temp]!=0) 为空的话(即字典里没有过这个键对)直接扔进存最终答案的vector里,更新总距离。然后判断这个位置加减1后如果仍然没出现在字典里,把这个位置的距离更新,扔进队列里。循环结束的条件是vector的大小等于m。最终输出就可以了。

(第一次写博客,写的不好还请见谅

#include<bits/stdc++.h>
using namespace std;
int n,m;
int x[]={};
vector<int>v;
queue<int>q;
map<int,int>d;
int main()
{
cin>>n>>m;
int i;
for(i=;i<=n;i++)
{
scanf("%d",&x[i]);
q.push(x[i]);
d[x[i]]=;
}
long long tot_dis=;
int cnt=;
while(!q.empty())
{
if(v.size()>=m)break;
int temp=q.front();
q.pop();
if(d[temp]!=)
{
tot_dis+=d[temp];
v.push_back(temp);
}
if(d.count(temp-)==)
{
d[temp-]=d[temp]+;
q.push(temp-);
}
if(d.count(temp+)==)
{
d[temp+]=d[temp]+;
q.push(temp+);
} }
cout<<tot_dis<<endl;
int j;
for(j=;j<v.size();j++)
{
cout<<v[j]<<' ';
}
return ;
}

Codeforces Round #611 (Div. 3) D的更多相关文章

  1. Codeforces Round #611 (Div. 3) A-F简要题解

    contest链接:https://codeforces.com/contest/1283 A. Minutes Before the New Year 题意:给一个当前时间,输出离第二天差多少分钟 ...

  2. Codeforces Round #611 (Div. 3)

    原题面:https://codeforces.com/contest/1283 A.Minutes Before the New Year 题目大意:给定时间,问距离零点零分还有多久? 分析:注意一下 ...

  3. Codeforces Round #611 (Div. 3) E

    Oh, New Year. The time to gather all your friends and reflect on the heartwarming events of the past ...

  4. Codeforces Round #611 (Div. 3) C

    There are nn friends who want to give gifts for the New Year to each other. Each friend should give ...

  5. Codeforces Round #366 (Div. 2) ABC

    Codeforces Round #366 (Div. 2) A I hate that I love that I hate it水题 #I hate that I love that I hate ...

  6. Codeforces Round #354 (Div. 2) ABCD

    Codeforces Round #354 (Div. 2) Problems     # Name     A Nicholas and Permutation standard input/out ...

  7. Codeforces Round #368 (Div. 2)

    直达–>Codeforces Round #368 (Div. 2) A Brain’s Photos 给你一个NxM的矩阵,一个字母代表一种颜色,如果有”C”,”M”,”Y”三种中任意一种就输 ...

  8. cf之路,1,Codeforces Round #345 (Div. 2)

     cf之路,1,Codeforces Round #345 (Div. 2) ps:昨天第一次参加cf比赛,比赛之前为了熟悉下cf比赛题目的难度.所以做了round#345连试试水的深浅.....   ...

  9. Codeforces Round #279 (Div. 2) ABCDE

    Codeforces Round #279 (Div. 2) 做得我都变绿了! Problems     # Name     A Team Olympiad standard input/outpu ...

随机推荐

  1. js动态创建的select2标签样式加载不上解决办法

    js动态创建的select2标签样式加载不上:调用select2的select2()函数来初始化一下: js抛出了Uncaught query function not defined for Sel ...

  2. [USACO13JAN]Cow Lineup

    Description Luogu3069 USACO Solution 由于两个点之间最多可以有\(k+1\)种牛,而牛的种数是单调的.所以可以用尺取法(区间伸缩法),每次右移右端点后,让左端点不断 ...

  3. P2141珠心算测验题解

    先来看一下题目:某学校的珠心算老师采用一种快速考察珠心算加法能力的测验方法.他随机生成一个正整数集合,集合中的数各不相同,然后要求学生回答:其中有多少个数,恰好等于集合中另外两个(不同的)数之和? 这 ...

  4. Vue - 动态组件 & 异步组件

    动态组件 <div id="app"> <components :is="com[2]"></components> < ...

  5. 二叉树的详细实现 (C++)

    二叉树的定义     以递归形式给出的:一棵二叉树是结点的一个有限集合,该集合或者为空,或者是由一个根结点加上两棵分别称为左子树和右子树的.互不相交的二叉树组成.二又树的特点是每个结点最多有两个子女, ...

  6. 多对多三种创建方式、forms组件、cookies与session

    多对多三种创建方式.forms组件.cookies与session 一.多对多三种创建方式 1.全自动 # 优势:不需要你手动创建第三张表 # 不足:由于第三张表不是你手动创建的,也就意味着第三张表字 ...

  7. C语言 exit

    C语言 exit 在main函数中调用exit和return结果是一样的,但在子函数中调用return只是代表子函数终止了,在子函数中调用exit,那么程序终止. 案例 #include <st ...

  8. js 时间格式化工具类

    /** * 返回示例:0 天 4 小时 7 分钟 57 秒 * @param second 毫秒数 * @returns {String} 时间html */ function secondToDay ...

  9. Docker - 创建第一个 docker 实例

    1. 概述 安装完准备开始使用 2. 环境 os centos 7 docker docker - ce 19.03 3. 步骤 启动docker > systemctl start docke ...

  10. npm报错This is probably not a problem with npm. There is likely additional logging

    使用webstorm开发时,遇到npm 报错,于是尝试了如下所有的方法,不完全统计. https://blog.csdn.net/liu305088020/article/details/791823 ...