CF1283D.Christmas Trees

普及+/提高

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

There are nn Christmas trees on an infinite number line. The ii-th tree grows at the position xix_i. All xix_i 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,…,ymy_1, y_2, \dots, y_m be the positions of people (note that all values x1,x2,…,xn,y1,y2,…,ymx_1, x_2, \dots, x_n, y_1, y_2, \dots, y_m should be distinct and all yjy_j should be integer). You want to find such an arrangement of people that the value ∑j=1mmin⁡i=1n∣xi−yj∣\sum\limits_{j=1}^{m}\min\limits_{i=1}^{n}|x_i - y_j| 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 djd_j be the distance from the jj-th human to the nearest Christmas tree (dj=min⁡i=1n∣yj−xi∣d_j = \min\limits_{i=1}^{n} |y_j - x_i|). Then you need to choose such positions y1,y2,…,ymy_1, y_2, \dots, y_m that ∑j=1mdj\sum\limits_{j=1}^{m} d_j is the minimum possible.

一条无限长的数轴上有 nn 棵圣诞树。第 ii 棵树生长在位置 xix_i 处。所有 xix_i 互不相同。

每个整数点最多只能被一棵圣诞树、一个人占据,或完全空置;非整数点不能被任何东西占据。

有 mm 个人希望庆祝圣诞节。设 y1,y2,…,ymy_1, y_2, \dots, y_m 表示这些人的位置(注意:所有值 x1,x2,…,xn,y1,y2,…,ymx_1, x_2, \dots, x_n, y_1, y_2, \dots, y_m 必须互不相同,且所有 yjy_j 均为整数)。你需要找出一种人员位置安排方案,使得 ∑j=1mmin⁡i=1n∣xi−yj∣\sum\limits_{j=1}^{m}\min\limits_{i=1}^{n}|x_i - y_j| 达到最小可能值(即:所有人到其最近的圣诞树的距离之和最小)。

换言之,令 djd_j 表示第 jj 个人到其最近的圣诞树的距离(即 dj=min⁡i=1n∣yj−xi∣d_j = \min\limits_{i=1}^{n} |y_j - x_i|)。那么你需要选择位置 y1,y2,…,ymy_1, y_2, \dots, y_m,使得 ∑j=1mdj\sum\limits_{j=1}^{m} d_j 最小。

输入格式

The first line of the input contains two integers nn and mm (1≤n,m≤2⋅1051 \le n, m \le 2 \cdot 10^5) — the number of Christmas trees and the number of people.

The second line of the input contains nn integers x1,x2,…,xnx_1, x_2, \dots, x_n (−109≤xi≤109-10^9 \le x_i \le 10^9), where xix_i is the position of the ii-th Christmas tree. It is guaranteed that all xix_i are distinct.

输入的第一行包含两个整数 nn 和 mm(1≤n,m≤2⋅1051 \le n, m \le 2 \cdot 10^5)—— 分别表示圣诞树的数量和人数。

输入的第二行包含 nn 个整数 x1,x2,…,xnx_1, x_2, \dots, x_n(−109≤xi≤109-10^9 \le x_i \le 10^9),其中 xix_i 表示第 ii 棵圣诞树的位置。保证所有 xix_i 互不相同。

输出格式

In the first line print one integer resres — the minimum possible value of ∑j=1mmin⁡i=1n∣xi−yj∣\sum\limits_{j=1}^{m}\min\limits_{i=1}^{n}|x_i - y_j| (in other words, the sum of distances to the nearest Christmas tree for all people).

In the second line print mm integers y1,y2,…,ymy_1, y_2, \dots, y_m (−2⋅109≤yj≤2⋅109-2 \cdot 10^9 \le y_j \le 2 \cdot 10^9), where yjy_j is the position of the jj-th human. All yjy_j should be distinct and all values x1,x2,…,xn,y1,y2,…,ymx_1, x_2, \dots, x_n, y_1, y_2, \dots, y_m should be distinct.

If there are multiple answers, print any of them.

第一行输出一个整数 resres —— ∑j=1mmin⁡i=1n∣xi−yj∣\sum\limits_{j=1}^{m}\min\limits_{i=1}^{n}|x_i - y_j| 的最小可能值(即所有人到最近的圣诞树的距离之和)。

第二行输出 mm 个整数 y1,y2,…,ymy_1, y_2, \dots, y_m(满足 −2⋅109≤yj≤2⋅109-2 \cdot 10^9 \le y_j \le 2 \cdot 10^9),其中 yjy_j 表示第 jj 个人的位置。所有 yjy_j 必须互不相同,且所有值 x1,x2,…,xn,y1,y2,…,ymx_1, x_2, \dots, x_n, y_1, y_2, \dots, y_m 也必须互不相同。

若存在多个答案,输出任意一个即可。

输入输出样例

  • 输入#1

    2 6
    1 5

    输出#1

    8
    -1 2 6 4 0 3
  • 输入#2

    3 5
    0 3 1

    输出#2

    7
    5 -2 4 -1 2

输入解题思路,AI测评打分。不知道怎么写?

首页