Codeforces Round #611 (Div. 3) 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.
一开始理解错题面了,用中位数思路写了一通发现连样例都过不了。这个题的意思是确定n个的位置(人和人,人和树的位置不能有重合),使每个人距离最近的圣诞树的距离的和最小。首先想到把人安排在圣诞树两边(人的位置为树的位置+-1),两边如果被占据的话继续往两边移。因为题目求的是和最小,容易想到bfs的思想(搜到终点能保证最小),所以在此处利用bfs,并利用map来存储当前位置到最近的树的距离。首先把所有树的坐标扔进队列。从队列取数后,先判断这个位置是否为空位置:if(d[temp]!=0) 为空的话(即字典里没有过这个键对)直接扔进存最终答案的vector里,更新总距离。然后判断这个位置加减1后如果仍然没出现在字典里,把这个位置的距离更新,扔进队列里。循环结束的条件是vector的大小等于m。最终输出就可以了。
(第一次写博客,写的不好还请见谅
#include<bits/stdc++.h>
using namespace std;
int n,m;
int x[]={};
vector<int>v;
queue<int>q;
map<int,int>d;
int main()
{
cin>>n>>m;
int i;
for(i=;i<=n;i++)
{
scanf("%d",&x[i]);
q.push(x[i]);
d[x[i]]=;
}
long long tot_dis=;
int cnt=;
while(!q.empty())
{
if(v.size()>=m)break;
int temp=q.front();
q.pop();
if(d[temp]!=)
{
tot_dis+=d[temp];
v.push_back(temp);
}
if(d.count(temp-)==)
{
d[temp-]=d[temp]+;
q.push(temp-);
}
if(d.count(temp+)==)
{
d[temp+]=d[temp]+;
q.push(temp+);
} }
cout<<tot_dis<<endl;
int j;
for(j=;j<v.size();j++)
{
cout<<v[j]<<' ';
}
return ;
}
Codeforces Round #611 (Div. 3) D的更多相关文章
- Codeforces Round #611 (Div. 3) A-F简要题解
contest链接:https://codeforces.com/contest/1283 A. Minutes Before the New Year 题意:给一个当前时间,输出离第二天差多少分钟 ...
- Codeforces Round #611 (Div. 3)
原题面:https://codeforces.com/contest/1283 A.Minutes Before the New Year 题目大意:给定时间,问距离零点零分还有多久? 分析:注意一下 ...
- Codeforces Round #611 (Div. 3) E
Oh, New Year. The time to gather all your friends and reflect on the heartwarming events of the past ...
- Codeforces Round #611 (Div. 3) C
There are nn friends who want to give gifts for the New Year to each other. Each friend should give ...
- Codeforces Round #366 (Div. 2) ABC
Codeforces Round #366 (Div. 2) A I hate that I love that I hate it水题 #I hate that I love that I hate ...
- Codeforces Round #354 (Div. 2) ABCD
Codeforces Round #354 (Div. 2) Problems # Name A Nicholas and Permutation standard input/out ...
- Codeforces Round #368 (Div. 2)
直达–>Codeforces Round #368 (Div. 2) A Brain’s Photos 给你一个NxM的矩阵,一个字母代表一种颜色,如果有”C”,”M”,”Y”三种中任意一种就输 ...
- cf之路,1,Codeforces Round #345 (Div. 2)
cf之路,1,Codeforces Round #345 (Div. 2) ps:昨天第一次参加cf比赛,比赛之前为了熟悉下cf比赛题目的难度.所以做了round#345连试试水的深浅..... ...
- Codeforces Round #279 (Div. 2) ABCDE
Codeforces Round #279 (Div. 2) 做得我都变绿了! Problems # Name A Team Olympiad standard input/outpu ...
随机推荐
- unittest的命令执行
命令窗口执行: 1.可以在命令窗口下执行单个module.class.method python -m unittest test_module1 test_module2 python -m uni ...
- 三分钟快速上手TensorFlow 2.0 (上)——前置基础、模型建立与可视化
本文学习笔记参照来源:https://tf.wiki/zh/basic/basic.html 学习笔记类似提纲,具体细节参照上文链接 一些前置的基础 随机数 tf.random uniform(sha ...
- sublime3使用技巧
1.鼠标悬浮,显示文件引用 Preference ——> Settings ——> "index_files": true (保存,重新打开即可) 2 ...
- 吴裕雄 python 机器学习——KNN回归KNeighborsRegressor模型
import numpy as np import matplotlib.pyplot as plt from sklearn import neighbors, datasets from skle ...
- C++ 宏定义创建(销毁)单例
Util.h: #define CREATE_SINGLETON_POINTER(CLASS,INSTANCE,MUTEX) if (NULL == INSTANCE) \ { \ MUTEX.loc ...
- nginx配置访问密码,输入用户名和密码才能访问
1. 安装 htpasswd 工具 yum install httpd-tools -y 设置用户名和密码,并把用户名.密码保存到指定文件中: [sandu@bogon conf]$ sudo mkd ...
- 报bug mui分享微信 ,qq 分享失败
1. iOS分享链接到微信,分享成功,但是分享的只有title,其他参数都没有 原因是mui分享进行了更新,msg新增了 msg.type 这个配置参数 qq : msg.type='text' 微信 ...
- leetCode练题——7. Reverse Integer
1.题目: 7. Reverse Integer Given a 32-bit signed integer, reverse digits of an integer. Example 1: I ...
- Error: EACCES: permission denied, open '/Users/qinmengjiao/WebstormProjects/m-kbs-app/.babelrc
表示没有访问这个文件的权限 执行命令 sudo chown -R $(whoami) ~/WebstormProjects/m-kbs-app/.babelrc 就可以解决上面的问题 以下是chown ...
- PP Bottle Have High Cycle Times
This year, the participation of 0.1% -0.4% sorbitol nucleating agent in general PP can produce high- ...