div3#611D Christmas Trees

Christmas Trees

题目描述

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 𝑦1,𝑦2,…,𝑦𝑚y1,y2,…,ym be the positions of people (note that all values 𝑥1,𝑥2,…,𝑥𝑛,𝑦1,𝑦2,…,𝑦𝑚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 ∑𝑗=1𝑚min𝑖=1𝑛|𝑥𝑖−𝑦𝑗|∑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 (𝑑𝑗=min𝑖=1𝑛|𝑦𝑗−𝑥𝑖|dj=mini=1n|yj−xi|). Then you need to choose such positions 𝑦1,𝑦2,…,𝑦𝑚y1,y2,…,ym that ∑𝑗=1𝑚𝑑𝑗∑j=1mdj is the minimum possible.

Input

The first line of the input contains two integers 𝑛n and 𝑚m (1≤𝑛,𝑚≤2⋅1e51≤n,m≤2⋅1e5) — the number of Christmas trees and the number of people.

The second line of the input contains 𝑛n integers 𝑥1,𝑥2,…,𝑥𝑛x1,x2,…,xn (−1e9≤𝑥𝑖≤1e9−1e9≤xi≤1e9), 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 ∑𝑗=1𝑚min𝑖=1𝑛|𝑥𝑖−𝑦𝑗|∑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 𝑦1,𝑦2,…,𝑦𝑚y1,y2,…,ym (−2⋅1e9≤𝑦𝑗≤2⋅1e9), where 𝑦𝑗yj is the position of the 𝑗j-th human. All 𝑦𝑗yj should be distinct and all values 𝑥1,𝑥2,…,𝑥𝑛,𝑦1,𝑦2,…,𝑦𝑚x1,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 

题意

给定n个圣诞树的坐标,让找出m个人的坐标,使得每一个人到其最近的圣诞树距离之和最小,并输出这些坐标。所有圣诞树和人的坐标两两不能相同。

分析

让每一个圣诞树依次往左右两端一步一步扩展,如果新坐标没有占用,就作为人的坐标。将新坐标再放入队列中继续扩展,直到得到了m个坐标时结束

坑点

由于自己固定思维太严重,一直以为unordered_map很快,吃了大亏,wa12了,后来改成了set就ac了,374ms过了

ac代码

#include <iostream>
#include <algorithm>
#include <queue>
#include <set>
using namespace std;
const int maxn = 1e6+10;
typedef pair<int,int> pii;
int N,M;
int arr[maxn],res[maxn],idx;
long long dis;
set<int> st;

void bfs(){
    queue<pii> q;
    for(int i = 0;i<N;i++) {
        q.push({arr[i],arr[i]});
        st.insert(arr[i]);
    }
    while(!q.empty()){
        pii cur = q.front();q.pop();
        int u = cur.first,v = cur.second;
        if(st.find(v+1) == st.end()){
            dis += abs(v+1-u);
            res[idx++] = v+1;
            st.insert(v+1);
            q.push({u,v+1});
        }
        if(idx >= M) break;
        if(st.find(v-1) == st.end()){
            dis += abs(v-1-u);
            res[idx++] = v-1;
            st.insert(v-1);
            q.push({u,v-1});
        }
        if(idx >= M) break;
    }
}
int main(){
    cin>>N>>M;
    for(int i = 0;i<N;i++) {
        scanf("%d",&arr[i]);
    }
    bfs();
    cout<<dis<<endl;
    for(int i = 0;i<idx;i++){
        printf("%d ",res[i]);
    }
    return 0;
}
全部评论

相关推荐

一共一个小时,面试难度以及自己的回答算是最近的面试压力比较大的,实习问了30分钟,中间穿插八股。1.redis数据结构2.redis持久化机制3.mysql索引底层4.聚簇索引与非聚簇索引5.索引优化6.索引失效7.mysql执行一条sql8.那么多索引mysql怎么选(不会)9.tcp与udp区别10.tcp为什么可靠11.消息队列作用12.kafka怎么保证消息有序性13.mcp是什么?14.skills是什么?15.jvm内存分配与回收过程(我讲了从创建对象到判断垃圾对象到垃圾回收我全说了一遍,是这个吗?)16.fullgc触发机制17.tcp的拥塞控制流程(不会了)18.分布式事务解决方案,说了2pc,3pc,tcc。算法是反转双向链表,没有按格式输出,但是面试官没让继续写了,面完以为挂了,结果晚上秒过,看看复试什么情况吧。今天百度打电话准备发offer了,业务跟在手子的差不多,很垂,并且说不分日常暑期,只看表现,会有转正机会,但是考虑再三还是拒绝了,百度实习薪资确实有点低,title也不如之前了,但是面试的二位业务老师我很喜欢,对我的评价也不错,希望之后能有机会共事。从三月份到现在一共面了六家,面试次数总共是8场,情况如下:脉脉二面(无答复,默认挂)百度二面已oc美团一面过,下周一二面shein一面过直接HR面游族一面过直接HR面腾讯一面过等待约二面滴滴明天一面面试通过率还是蛮高的,但是大部分都是日常,感觉对我现在的加成不大,大概率不会去,不知道暑期会是什么情况呢唉,希望能有面试吧,继续加油。字节被无hc直接取消了,现在还没人捞,有没有字节HR救救我
不管什么都不想跳动了:本人美团百度快手都待过,建议肯定是直接留快手多一点产出后转正or直接冲字节腾讯暑期吧。一是快手从福利到基建都吊打另外两家。美团现在这个业务比较惨,本来毛利就很低,亏损严重,今年很可能要优化人力降低成本,去了别说日常,就算暑期后面都很可能被优化。百度其实实习生权限挺高的,可以接触到一些含金量高的项目,但是现在的风评不如之前了,薪资也不高。二是转正概率和薪资是跟产出挂钩的,你都在手子已经积累产出了,去其他家日常实习产出都是从0开始,肯定不可能有你在手子转正可能性大啊,现在日常压根没必要去,而且我有两个师弟都是在快手日常转正的,不用太担心,安心留在手子一边多做一点产出然后一边冲字节腾讯暑期,字节腾讯今年实习岗位非常多的,不如好好把握这个,加油。
今天你投了哪些公司?
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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