链接: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]);
}