codeforces 722D. Generating Sets

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u011528035/article/details/52721790

题意:X集合的数 可以有两种方式变换Y  现在给你一个集合Y   求X 且这个X的里最大的数 尽可能小 (由于是集合 所以集合里的元素相同)


思路:贪心

假设现在是 a>d>b>c  

但是a和d有可能变到c

由于我们是要把最大的数变的尽可能小  

所以对于任意情况下  必然是变化a比较合适   但如果a都变不了其实就代表结束了

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <map>
#include <queue>
using namespace std;

#define N 100100
#define LL long long

int a[50100];
priority_queue<int> q;
map<int,int> mp;
int b[50100];

int main()
{
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&a[i]);
        q.push(a[i]);
        mp[a[i]]=1;
    }

    while(!q.empty())
    {
        int t=q.top();
        int f=t;
        while(mp[t]&&t!=0)
        {
            t/=2;
        }
        if(t==0)
        {
            break;
        }
        mp[f]=0;
        mp[t]=1;
        q.pop();
        q.push(t);
    }
    while(!q.empty()){
        printf("%d ",q.top());
        q.pop();
    }
    return 0;
}


猜你喜欢

转载自blog.csdn.net/u011528035/article/details/52721790
今日推荐