标签:cpp pair front amp https round i++ lld 距离
题意:现在已知圣诞树在x位置,求人的位置,使每个人到达距离他最近的树的距离之和最小
安排人肯定从树旁边开始安排,树的距离为1的位置安排满之后继续安排下一层,因为线无线长,也没什么限制,就直接bfs
map<int, bool> vis;
queue<pair<int, int>> q;
vector<int> v;
int main()
{
int n, m;cin>>n>>m;
for (int i = 0, x; i < n; i++)
{
cin>>x;vis[x] = 1;
q.push({1, x + 1}),q.push({1, x - 1});
}
ll ans = 0;
while (m > 0 && !q.empty())
{
pii x = q.front(); q.pop();
if (!vis[x.second])
{
ans += x.first; m--;
v.push_back(x.second);
vis[x.second] = 1;
q.push({x.first + 1, x.second + 1});
q.push({x.first + 1, x.second - 1});
}
}
printf("%lld\n", ans);
for (int i = 0; i < v.size(); i++)
printf("%d ", v[i]);
return 0;
}
Codeforces Round #611 (Div. 3) D - Christmas Trees(BFS)
标签:cpp pair front amp https round i++ lld 距离
原文地址:https://www.cnblogs.com/Herlo/p/12114301.html