• codeforces 1283D BFS


    太久没写bfs,写法想错了算错了复杂度

    题意:x坐标上,给了n个圣诞树的坐标,让你再求出m个人点,使得这m个人的点到最近的圣诞树的和最小,这n+m个点distinct。

    思路:之间裸bfs。因为x1e9,可以用map<int,int> vis;//用map记录是否访问过。

    #include <iostream>
    #include <cmath>
    #include <cstdio>
    #include <cstring>
    #include <string>
    #include <map>
    #include <iomanip>
    #include <algorithm>
    #include <queue>
    #include <stack>
    #include <set>
    #include <vector> 
    // #include <bits/stdc++.h>
    #define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    #define sp ' '
    #define endl '
    '
    #define inf  0x3f3f3f3f;
    #define FOR(i,a,b) for( int i = a;i <= b;++i)
    #define bug cout<<"--------------"<<endl
    #define P pair<int, int>
    #define fi first
    #define se second
    #define pb(x) push_back(x)
    #define ppb() pop_back()
    #define mp(a,b) make_pair(a,b)
    #define ms(v,x) memset(v,x,sizeof(v))
    #define rep(i,a,b) for(int i=a;i<=b;i++)
    #define repd(i,a,b) for(int i=a;i>=b;i--)
    #define sca3(a,b,c) scanf("%d %d %d",&(a),&(b),&(c))
    #define sca2(a,b) scanf("%d %d",&(a),&(b))
    #define sca(a) scanf("%d",&(a));
    #define sca3ll(a,b,c) scanf("%lld %lld %lld",&(a),&(b),&(c))
    #define sca2ll(a,b) scanf("%lld %lld",&(a),&(b))
    #define scall(a) scanf("%lld",&(a));
    
    
    using namespace std;
    typedef long long ll;
    ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
    ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
    ll powmod(ll a, ll b, ll mod){ll sum = 1;while (b) {if (b & 1) {sum = (sum * a) % mod;b--;}b /= 2;a = a * a % mod;}return sum;}
    
    const double Pi = acos(-1.0);
    const double epsilon = Pi/180.0;
    const int maxn = 2e5+10;
    map<int,int> vis;//用map记录是否访问过
    ll a[maxn];
    int n,m;
    ll sum = 0;
    priority_queue<pair<ll,ll> > que;
    queue<ll>ans;
    void solve()
    {
        int cnt = 0;
        while(que.size()&&ans.size()<m)
        {
    
            int d = que.top().fi;
            d = -d;
            int pos = que.top().se;
            que.pop();
            if(vis[pos]==1) continue;
            vis[pos]=1;
            ans.push(pos);
            sum += d;
            que.push(mp(-(d+1),pos-1));
            que.push(mp(-(d+1),pos+1));
        }
    }
    int main()
    {
        //freopen("input.txt", "r", stdin);
        cin>>n>>m;
        rep(i,1,n){
            cin>>a[i];
            vis[a[i]]=1;
        }
        rep(i,1,n){
            que.push(mp(-1,a[i]-1));
            que.push(mp(-1,a[i]+1));
        }
        solve();
        cout<<sum<<endl;
        while(ans.size()){
            int x = ans.front();
            ans.pop();
            cout<<x<<sp;
        }
    
    
    
    }
  • 相关阅读:
    前端笔记7
    前端笔记6
    列表、行块元素
    我的学习方法(5)
    (读后摘抄)《计算机程序设计语言的发展》_王汝传
    电影《比利·林恩的中场战事》
    关于优化(1)
    Debug技巧(1)
    奇奇怪怪的东西(1)
    我的学习方法(4)
  • 原文地址:https://www.cnblogs.com/jrfr/p/13259698.html
Copyright © 2020-2023  润新知