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 ...
随机推荐
- YAML(YML)语法详解
ansible playbook是由yaml(yml)语法书写,结构清晰,可读性强,所以必须掌握yaml(yml)基础语法 语法 描述 锁进 YAML使用固定的缩进风格表示层级结构,每个缩进由两个空 ...
- winform学习(1)初识winform
winform是Windows窗体应用程序 在窗体设计界面 单击鼠标右键--查看代码,即可转到Form1.cs的代码界面 从代码界面转到窗体设计界面的三种快捷方法:①双击解决方案资源管理器的 For ...
- java is 和 == ,以及equal
package string; public class MemAddrChange { public static void main(String[] args) { // const 常量区,
- apache+SSL 搭建https
简单介绍 一般情况下,我们打开网站默认的是使用明文传输方式,但在日常生活中,当我们在登录或者支付交易时,网站就会自动跳转至SSL(Secure Sockets Layes)加密传输模式,SSL的功能就 ...
- 一些常用的js代码
跳转 window.location.href= 刷新 location.reload()
- summernote 上传图片到图片服务器的解决方案(springboot 成功)
遇到的可以连接成功但是拒绝登录的问题 前提说一下,我自己在自己的服务器上配置了nginx的反向代理,所以请求的时候才会直接写的是我的ip地址,要配置nginx的话,可以看我的nginx的笔记 当代码感 ...
- 30分钟编写一个抓取 Unsplash 图片的 Python爬虫
我一直想用 Python and Selenium 创建一个网页爬虫,但从来没有实现它. 几天前, 我决定尝试一下,这听起来可能是挺复杂的, 然而编写代码从 Unsplash 抓取一些美丽的图片 ...
- DRF分页
一.序列化 from rest_framework impost serializers from . models import * class GoodsSerializer(serializer ...
- C/S编程
https://blog.csdn.net/antony1776/article/details/73717666 实现C/S程序,加上登录注册聊天等功能. 然后要做个协议的样子出来. 比如说注册功能 ...
- 线性筛-euler,强大O(n)
欧拉函数是少于或等于n的数中与n互质的数的数目 φ(1)=1(定义) 类似与莫比乌斯函数,基于欧拉函数的积性 φ(xy)=φ(x)φ(y) 由唯一分解定理展开显然,得证 精髓在于对于积性的应用: ){ ...