CodeForces - 1283D Christmas Trees

48 篇文章 0 订阅
11 篇文章 0 订阅

思路:这题就是从每个圣诞树左右进行扩展,优先近的点

做法:每个点设一个坐标id,扩展方向f(+-1),扩展距离s,把每个点放到优先队列里,按照s小的在顶端,map纪录一下扩展过的点,依次从顶端取点扩展就好了

 There are n Christmas trees on an infinite number line. The i-th tree grows at the position xi. All xi

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 m

people who want to celebrate Christmas. Let y1,y2,…,ym be the positions of people (note that all values x1,x2,…,xn,y1,y2,…,ym should be distinct and all yj should be integer). You want to find such an arrangement of people that the value ∑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 dj

be the distance from the j-th human to the nearest Christmas tree (dj=mini=1n|yj−xi|). Then you need to choose such positions y1,y2,…,ym that ∑j=1mdj

is the minimum possible.

Input

The first line of the input contains two integers n

and m (1≤n,m≤2⋅105

) — the number of Christmas trees and the number of people.

The second line of the input contains n

integers x1,x2,…,xn (−109≤xi≤109), where xi is the position of the i-th Christmas tree. It is guaranteed that all xi

are distinct.

Output

In the first line print one integer res

— the minimum possible value of ∑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 m

integers y1,y2,…,ym (−2⋅109≤yj≤2⋅109), where yj is the position of the j-th human. All yj should be distinct and all values x1,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 

Sponsor

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int maxn = 501050;
struct node
{
    ll id,f,s;
    friend bool operator < (node n1, node n2)
    {
        return n1.s>n2.s;//自定义优先级从大到小
    }
} a[maxn];
map<ll,ll>mp;ll n,m;
priority_queue<node>q;
ll ans[maxn];
void slove()
{
    ll s=0;
    for(ll i=1;i<=m;i++)
    {
        node u=q.top();
        q.pop();
        while(mp.count(u.id+u.f*u.s)!=0)
        {
            u=q.top();
            q.pop();
        }

        ans[i]=u.id+u.f*u.s;
        s+=u.s;
        mp[ans[i]]=1;
        //printf("i=%lld u.id=%lld u.f=%lld u.s=%lld s=%lld\n",i,u.id,u.f,u.s,s);
        u.s+=1;
        q.push(u);
        //printf("ans[%lld]=%lld\n",i,ans[i]);
    }
    printf("%lld\n",s);
    for(ll i=1;i<=m;i++)
    {
        printf("%lld",ans[i]);
        if(i==m) printf("\n");
        else printf(" ");
    }
}
int main()
{

    scanf("%lld %lld",&n,&m);
    for(ll i=1;i<=n;i++)
    {
        ll x;
        scanf("%lld",&x);
        mp[x]=1;
        q.push({x,1,1});
        q.push({x,-1,1});
    }
    slove();
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值