D. Christmas Trees

52 篇文章 1 订阅

https://codeforces.com/problemset/problem/1283/D

题目描述

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

输入格式

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

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

输出格式

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

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

If there are multiple answers, print any of them.

题意翻译

有 nn 棵圣诞树和 mm 个人,第 ii 棵圣诞树所在的位置是 a_iai​ ,令 d(x)d(x) 表示第 xx 个人到离他最近的圣诞树的距离,现在需要你给每个人安排一个位置,使得 \sum\limits_{i=1}^m d(i)i=1∑m​d(i) 最小,并输出这个值以及每个人的位置

注意任何位置最多只能有一个人或者圣诞树

输入输出样例

输入 #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 

思路:模拟,比较容易想到把一个点的相邻两个点用完后然后再用下一个点的相邻两个点,以此类推,把距离为1的点用完了再去用距离为2的点,实际上是一个bfs的过程。所以使用bfs去模拟。

看了其他做法发现还有二分答案的..不是那么容易想到用二分判定去模拟这个题

#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<cstdio>
#include<algorithm>
#define debug(a) cout<<#a<<"="<<a<<endl;
using namespace std;
const int maxn=1e5;
typedef long long LL;
vector<LL>v;
queue< pair<LL,LL> >q;
map<LL,bool>vis;
int main(void)
{
  cin.tie(0);std::ios::sync_with_stdio(false);
  LL n,m;cin>>n>>m;
  for(LL i=1;i<=n;i++)
  {
  	LL x;cin>>x;
  	vis[x]=true;
  	q.push({1,x-1});
  	q.push({1,x+1});
  }
  LL ans=0;
  while(q.size())
  {
  	 pair<LL,LL> now=q.front();q.pop();
  	 if(!vis[now.second])
  	 {
  	 	m--;
  		ans+=now.first;
  		v.push_back(now.second);
  		if(m<=0) break;
		vis[now.second]=true;
		if(!vis[now.second+1]) q.push({now.first+1,now.second+1});
		if(!vis[now.second-1]) q.push({now.first+1,now.second-1});		 	
	 }
  }
  cout<<ans<<endl;
  for(LL i=0;i<v.size();i++) cout<<v[i]<<" ";
  cout<<endl;
return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值