CF722D 产生集合

1 CF722D 产生集合

2 题目描述

时间限制 \(2s\) | 空间限制 \(256M\)
You are given a set Y of n distinct positive integers \(y_1, y_2, ..., y_n.\)

Set \(X\) of \(n\) distinct positive integers \(x_1, x_2, ..., x_n\) 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\):

  • Take any integer \(x_i\) and multiply it by two, i.e. replace \(x_i\) with \(2\times x_i\).
  • Take any integer \(x_i\), multiply it by two and add one, i.e. replace \(x_i\) with \(2\times x_i + 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.

数据范围:\(1 ≤ n ≤ 50000, 1 ≤ y_i ≤ 10^9\)

3 题解

我们转化一下题目,相当于将给出的 \(y\) 序列中的所有数进行一定操作(使 \(t\) 变为 \(\lfloor \dfrac{t}{2} \rfloor\)),使得最终 \(y\) 序列变成的 \(x\) 序列中的最大值最小。容易想到,我们每一步一定都是想办法将 \(y\) 序列中的最大值变得尽可能小,因为最终序列的优先程度还是取决于其最大值的大小。我们如何判断一个数是否可以继续变小了呢?显然,我们无法将 \(1\) 变小,且最终构造出来的序列必须不可重复。因此,我们可以想到:每一次将最大值 \(maxn\) 变为 \(\lfloor \dfrac{maxn}{2} \rfloor\),然后查看是否存在当前值,如果存在就继续。如果我们最终停下来的地方是 \(1\),那么就说明我们无法将最大值变小,直接输出当前序列即可。否则我们可以将停止的位置到最大值这一路上所有的值都进行一次操作。具体地,我们可以利用 \(dfs\) 来查找停止的位置,然后在回溯时进行操作;用 \(Map\) 存储当前权值的位置,如果没有那就是 \(0\);用一棵维护单点修改、区间最大值的线段树来进行修改和找到当前序列的最大值。

4 代码(空格警告):

#include <iostream>
#include <queue>
#include <map>
using namespace std;
const int N = 5e4+10;
int n, maxn, now, y;
int a[N];
struct node
{
    int l, r, maxn;
}t[N*4];
map<int, int> M;
void build(int p, int l, int r)
{
    t[p].l = l;
    t[p].r = r;
    if (l == r)
    {
        t[p].maxn = a[l];
        return ;
    }
    int mid = (l+r)/2;
    build(p*2, l, mid);
    build(p*2+1, mid+1, r);
    t[p].maxn = max(t[p*2].maxn, t[p*2+1].maxn);
}
void modify(int p, int pos, int d)
{
    if (t[p].l == t[p].r)
    {
        t[p].maxn = d;
        return;
    }
    int mid = (t[p].l + t[p].r) / 2;
    if (pos <= mid) modify(p*2, pos, d);
    if (pos > mid) modify(p*2+1, pos, d);
    t[p].maxn = max(t[p*2].maxn, t[p*2+1].maxn);
}
int query(int p, int l, int r)
{
    if (t[p].l >= l && t[p].r <= r) return t[p].maxn;
    int mid = (t[p].l + t[p].r) / 2;
    int ans = -0x3f3f3f3f;
    if (l <= mid) ans = max(ans, query(p*2, l, r));
    if (r > mid) ans = max(ans, query(p*2+1, l, r));
    return ans;
}
void dfs(int x)
{
    if (M[x/2]) dfs(x/2);
    if (x == 1)
    {
        for (int i = 1; i <= n; i++) cout << query(1, i, i) << " ";
        exit(0);
    }
    M[x/2] = M[x];
    y = M[x];
    modify(1, y, x/2);
}
int main()
{
    cin >> n;
    for (int i = 1; i <= n; i++) cin >> a[i], M[a[i]] = i;
    build(1, 1, n);
    while (1)
    {
        maxn = query(1, 1, n);
        dfs(maxn);
    }
    return 0;
}

欢迎关注我的公众号:智子笔记

posted @ 2021-04-10 14:35  David24  阅读(124)  评论(0编辑  收藏  举报