codeforces-1283D(多源BFS)
发布日期:2022-03-30 18:18:16 浏览次数:40 分类:博客文章

本文共 2694 字,大约阅读时间需要 8 分钟。

Christmas Trees

time limit per test2 seconds

memory limit per test256 megabytes
inputstandard input
outputstandard output
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 nn and mm (1≤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), 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 61 5

output

Copy

8-1 2 6 4 0 3

input

Copy

3 50 3 1

output

Copy

75 -2 4 -1 2

题意:一列位置,给定n个树的坐标,求m个人的坐标使每个人到每棵树的距离加起来最小

题解:多源bfs,注意map标记

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;using ll = long long;const ll N = 1e6;const double PI = acos(-1.0);#define Test ll tesnum;tesnum = read();while(tesnum--)ll read();int main(){ int n,m,x; map
mp; queue
q; cin>>n>>m; for(int i = 0; i < n; i++){ cin>>x; mp[x] = 0; q.push(x); } ll ans = 0; vector
res; while(!q.empty()) { if(res.size()==m)break; int now = q.front();q.pop(); if(mp[now]!=0){ ans+=mp[now]; res.push_back(now); } if(!mp.count(now-1)){ mp[now-1] = mp[now]+1; q.push(now-1); } if(!mp.count(now+1)){ mp[now+1] = mp[now]+1; q.push(now+1); } } cout<
<

转载地址:https://www.cnblogs.com/cloudplankroader/p/12256167.html 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:codeforces-1285D(字典树)
下一篇:C++通用工具:pair和tuple

发表评论

最新留言

路过按个爪印,很不错,赞一个!
[***.219.124.196]2024年04月11日 02时46分15秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章