Codeforces 722D Generating Sets【优先队列+贪心】

本文介绍了一个算法问题,即如何构造一个最小最大值的整数集合X,通过特定操作能生成给定的整数集合Y。文章详细阐述了解决方案,包括问题分析、核心思路和AC代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

D. Generating Sets
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given a set Y of n distinct positive integers y1, y2, ..., yn.

Set X of n distinct positive integers x1, x2, ..., xn is said to generate set Y if one can transform X to Y by applying some number of the following two operation to integers in X:

  1. Take any integer xi and multiply it by two, i.e. replace xi with xi.
  2. Take any integer xi, multiply it by two and add one, i.e. replace xi with xi + 1.

Note that integers in X are not required to be distinct after each operation.

Two sets of distinct integers X and Y are equal if they are equal as sets. In other words, if we write elements of the sets in the array in the increasing order, these arrays would be equal.

Note, that any set of integers (or its permutation) generates itself.

You are given a set Y and have to find a set X that generates Y and the maximum element of X is mininum possible.

Input

The first line of the input contains a single integer n (1 ≤ n ≤ 50 000) — the number of elements in Y.

The second line contains n integers y1, ..., yn (1 ≤ yi ≤ 109), that are guaranteed to be distinct.

Output

Print n integers — set of distinct integers that generate Y and the maximum element of which is minimum possible. If there are several such sets, print any of them.

Examples
Input
5
1 2 3 4 5
Output
4 5 2 3 1 
Input
6
15 14 3 13 1 12
Output
12 13 14 7 3 1 
Input
6
9 7 13 17 5 11
Output
4 5 2 6 3 1 

题目大意:

让你构造一个长度为N的数组X【】,使得其能够通过两种操作得到数组Y【】;

现在给你一个数组Y【】,让你构造一个可行X【】,使得其中最大值尽可能的小。

两种操作:

选择x【i】,使得其变成X【i】*2或者是X【i】*2+1.....

保证Y【】是一个不包含重复元素的数组,要求X【】也不能包含重复元素。而且所有元素大于0.


思路:


1、考虑问题本质,其实我们最开始可以设定ans【】(X【】)就是Y【】;

然后尽可能的缩小最大值,来寻找一个最终序列,使得X【】中最大元素尽可能的小。


2、那么首先我们将数组Y【】从大到小排序,然后全部塞到一个优先队列中,每次我们拿出最大值,肯定想要做到的就是将这个数变小,变成一个和此时队列中所有元素不相同的小值。

那么我们每一次取队头元素,将其尽可能的变小,如果可以变小,再将变小的值塞进队列,继续取队头,循环往复。

直到不能变小为止,停止操作即可。


Ac代码:

#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<map>
#include<queue>
using namespace std;
struct node
{
    int val;
    friend bool operator <(node a,node b)
    {
        return a.val<b.val;
    }
}nod;
int a[50050];
int ans[50050];
int b[500];
int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        for(int i=0;i<n;i++)scanf("%d",&a[i]);
        sort(a,a+n);reverse(a,a+n);
        priority_queue<node>s;
        map<int ,int >ss;
        for(int i=0;i<n;i++)
        {
            nod.val=a[i];
            ss[a[i]]=1;
            s.push(nod);
        }
        while(1)
        {
            int flag=0;
            nod=s.top();
            while(nod.val)
            {
                nod.val/=2;
                if(ss[nod.val]==0&&nod.val!=0)
                {
                    flag=1;
                    ss[nod.val]=1;
                    s.pop();
                    s.push(nod);
                    break;
                }
            }
            if(flag==0)break;
        }
        while(!s.empty())
        {
            nod=s.top();
            s.pop();
            printf("%d ",nod.val);
        }
        printf("\n");
    }
}








当前提供的引用内容并未提及关于Codeforces比赛M1的具体时间安排[^1]。然而,通常情况下,Codeforces的比赛时间会在其官方网站上提前公布,并提供基于不同时区的转换工具以便参赛者了解具体开赛时刻。 对于Codeforces上的赛事而言,如果一场名为M1的比赛被计划举行,则它的原始时间一般按照UTC(协调世界时)设定。为了得知该场比赛在UTC+8时区的确切开始时间,可以遵循以下逻辑: - 前往Codeforces官网并定位至对应比赛页面。 - 查看比赛所标注的标准UTC起始时间。 - 将此标准时间加上8小时来获取对应的北京时间(即UTC+8)。 由于目前缺乏具体的官方公告链接或者确切日期作为依据,无法直接给出Codeforces M1比赛于UTC+8下的实际发生时段。建议定期访问Codeforces平台查看最新动态更新以及确认最终版程表信息。 ```python from datetime import timedelta, datetime def convert_utc_to_bj(utc_time_str): utc_format = "%Y-%m-%dT%H:%M:%SZ" bj_offset = timedelta(hours=8) try: # 解析UTC时间为datetime对象 utc_datetime = datetime.strptime(utc_time_str, utc_format) # 转换为北京时区时间 beijing_time = utc_datetime + bj_offset return beijing_time.strftime("%Y-%m-%d %H:%M:%S") except ValueError as e: return f"错误:{e}" # 示例输入假设某场Codeforces比赛定于特定UTC时间 example_utc_start = "2024-12-05T17:35:00Z" converted_time = convert_utc_to_bj(example_utc_start) print(f"Codeforces比赛在北京时间下将是:{converted_time}") ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值