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∑mi=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∑mdj 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∑mi=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∑md(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;
}