zoukankan      html  css  js  c++  java
  • codeforces-1283D(多源BFS)

    Christmas Trees

    time limit per test2 seconds
    memory limit per test256 megabytes
    inputstandard input
    outputstandard output
    There are n Christmas trees on an infinite number line. The i-th tree grows at the position xi. All xi 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 m people who want to celebrate Christmas. Let y1,y2,…,ym be the positions of people (note that all values x1,x2,…,xn,y1,y2,…,ym should be distinct and all yj should be integer). You want to find such an arrangement of people that the value ∑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 dj be the distance from the j-th human to the nearest Christmas tree (dj=mini=1n|yj−xi|). Then you need to choose such positions y1,y2,…,ym that ∑j=1mdj is the minimum possible.

    Input

    The first line of the input contains two integers nn and mm (1≤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), 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.

    Examples

    input

    Copy

    2 6
    1 5
    

    output

    Copy

    8
    -1 2 6 4 0 3 
    

    input

    Copy

    3 5
    0 3 1
    

    output

    Copy

    7
    5 -2 4 -1 2 
    

    题意:一列位置,给定n个树的坐标,求m个人的坐标使每个人到每棵树的距离加起来最小

    题解:多源bfs,注意map标记

    #include<iostream>
    #include<algorithm>
    #include<cstring>
    #include<cctype>
    #include<string>
    #include<stack>
    #include<queue>
    #include<set>
    #include<map>
    #include<cmath>
    #include<vector>
    
    using namespace std;
    using ll = long long;
    const ll N = 1e6;
    const double PI = acos(-1.0);
    #define Test ll tesnum;tesnum = read();while(tesnum--)
    ll read();
    
    int main()
    {
        int n,m,x;
        map<int,int> mp;
        queue<int> q;
        cin>>n>>m;
        for(int i = 0; i < n; i++){
            cin>>x;
            mp[x] = 0;
            q.push(x);
        }
        ll ans = 0;
        vector<int> res;
        while(!q.empty())
        {
            if(res.size()==m)break;
            int now = q.front();q.pop();
            if(mp[now]!=0){
                ans+=mp[now];
                res.push_back(now);
            }
            if(!mp.count(now-1)){
                mp[now-1] = mp[now]+1;
                q.push(now-1);
            }
            if(!mp.count(now+1)){
                mp[now+1] = mp[now]+1;
                q.push(now+1);
            }
        }
        cout<<ans<<endl;
        for(auto a:res){
            cout<<a<<" ";
        }cout<<endl;
        return "BT7274", NULL;
    }
    
    inline ll read() {
        ll hcy = 0, dia = 1;char boluo = getchar();
        while (!isdigit(boluo)) {if (boluo == '-')dia = -1;boluo = getchar();}
        while (isdigit(boluo)) {hcy = hcy * 10 + boluo - '0';boluo = getchar();}
        return hcy * dia;
    }
    
  • 相关阅读:
    VC6.0 error LNK2001: unresolved external symbol _main解决办法
    C++中数字与字符串之间的转换(使用CString.Format或者sprintf)
    C++内存管理(超长)
    Flash, Flex, Air, Flashplayer之间的相互关系是什么?
    《KVM虚拟化技术实战和原理解析》读书笔记(十几篇)
    HNCU1323:算法2-1:集合union (线性表)
    Delphi XE7下如何创建一个Android模拟器调试
    DelphiXE Android的所有权限按照分类总结说明
    DelphiXE8怎么使用调试模式(朱建强)
    C++静态库中使用_declspec(dllexport) 不能导出函数的问题
  • 原文地址:https://www.cnblogs.com/cloudplankroader/p/12256167.html
Copyright © 2011-2022 走看看