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 ...
随机推荐
- 【常识】常用RGB颜色对照表
RGB颜色表 白色:rgb(255,255,255) 黑色:rgb(0,0,0) 红色:rgb(255,0,0) 绿色:rgb(0,255,0) 蓝色:rgb(0,0,255) 青色:rgb(0,25 ...
- CSS和JS两种颜色渐变文字效果代码
js实现颜色渐变文字效果代码: <!-- js颜色渐变色文字 --> <div id="moml"> <div style="text-al ...
- robots.txt 让搜索引擎不再收录网站
有没有担心过自己的隐私会在强大的搜索引擎面前无所遁形?想象一下,如果要向世界上所有的人公开你的私人日记,你能接受吗?的确是很矛盾的问题,站长们大都忧虑“如何让搜索引擎收录的我的网站?”,而我们还是要研 ...
- 微信小程序 购物车流程
购物车流程 一.需求分析 a:全选,单选,根据选中的计算数目和总价 b:单个商品加减 c:删除一个商品 wxml 布局 <view> <view v-if="flag&qu ...
- nmon+python 基于AIX系统数据分析
https://sourceforge.net/projects/pynmongraph/ github :https://github.com/madmaze/pyNmonAnalyzer nmon ...
- opencv:图像噪声
常见噪声的类型: 椒盐噪声 高斯噪声 其他噪声...... 手动生成图像噪声: #include <opencv2/opencv.hpp> #include <iostream> ...
- 2.1 【配置环境】 JDK + eclipse + selenium
1.jdk以及eclipse的具体安装详见 http://www.cnblogs.com/ericazy/p/6082194.html 安装1.7 jdk即可 2.selenium 旧版本安装: s ...
- 无源汇有上下界可行流(ZQU 1590)
无源汇有上下界可行流(也就是循环流) 模型:一个网络,求出一个流,使得每条边的流量必须>=Li且<=Hi, 每个点必须满足总流入量=总流出量(流量守恒)(这个流的特点是循环往复,无始无终) ...
- Django模板结构优化{% include %}和{% extend %}标签
https://blog.csdn.net/xujin0/article/details/83420633
- kvm增加磁盘容量
一.qcow2格式 查看镜像文件实际占用空间 ls -alh t.qcow2 加容量(只能加不能减) qemu-img resize t.qcow2 +1G 查看qcow2信息 qemu-img in ...