722D - Generating Sets - CodeForces Solution


binary search data structures dfs and similar greedy strings trees *1900

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
using namespace std;
#define YN(result) (result)? cout<<"YES\n" : cout<<"NO\n";
typedef long long int ll;
long long int const MOD = 1e9 + 7;
int const N = 2e5 + 7;

void solve()
{
    int n; cin >> n;
    vector <ll> v(n);
    for(auto &x : v) cin >> x;
    set <ll> ans;
    priority_queue <ll> q;
    sort(v.rbegin(), v.rend());
    for(auto x : v){
        ans.insert(x);
        q.push(x);
    }
    ll op = 0;
    while(1){
        ll a = q.top();
        ll b = a;
        int loop = 0;
        while(ans.count(a) != 0 && a > 0){
            if(a & 1) a = (a - 1) / 2;
            else a = a / 2;
            if(a == 0) break;
            if(ans.count(a) == 0) op = 0;
        }
        
        if(a > 0){
            ans.erase(b);
            ans.insert(a);
            q.push(a);
        }
        op++;
        q.pop();
        if(op == 2 * n) break;
    }
    for(auto x : ans){
        cout << x << " ";
    }
    cout << "\n";
}

int main()
{
    ios_base::sync_with_stdio(false); cin.tie(NULL);
    int t = 1;
    // cin >> t;
    while(t--){
        solve();
    }
    return 0;
}
  		     	       	   		 	 		


Comments

Submit
0 Comments
More Questions

230A - Dragons
200B - Drinks
13A - Numbers
129A - Cookies
1367B - Even Array
136A - Presents
1450A - Avoid Trygub
327A - Flipping Game
411A - Password Check
1520C - Not Adjacent Matrix
1538B - Friends and Candies
580A - Kefa and First Steps
1038B - Non-Coprime Partition
43A - Football
50A - Domino piling
479A - Expression
1480A - Yet Another String Game
1216C - White Sheet
1648A - Weird Sum
427A - Police Recruits
535A - Tavas and Nafas
581A - Vasya the Hipster
1537B - Bad Boy
1406B - Maximum Product
507B - Amr and Pins
379A - New Year Candles
1154A - Restoring Three Numbers
750A - New Year and Hurry
705A - Hulk
492B - Vanya and Lanterns