Codeforces722D-Generating Sets(set + 二分答案)

25 篇文章 0 订阅
7 篇文章 0 订阅

题目链接

http://codeforces.com/contest/722/problem/D

思路

最大值最小问题,显然二分
二分一下最大值的大小,然后每次去judge
judge的思路:设上限为y,对于一个元素x,首先缩小到 xy ,然后如果集合里面已经存在x了,那么 x=x/2 ,直到能够插入x

细节

答案是在judge函数里面插入的,二分的时候如果judge(L) M = R,此时没有进入judge函数,于是没有输出= =

代码

#include <iostream>
#include <cstring>
#include <stack>
#include <vector>
#include <set>
#include <map>
#include <cmath>
#include <queue>
#include <sstream>
#include <iomanip>
#include <fstream>
#include <cstdio>
#include <cstdlib>
#include <climits>
#include <deque>
#include <bitset>
#include <algorithm>
using namespace std;

#define PI acos(-1.0)
#define LL long long
#define PII pair<int, int>
#define PLL pair<LL, LL>
#define mp make_pair
#define IN freopen("in.txt", "r", stdin)
#define OUT freopen("out.txt", "wb", stdout)
#define scan(x) scanf("%d", &x)
#define scan2(x, y) scanf("%d%d", &x, &y)
#define scan3(x, y, z) scanf("%d%d%d", &x, &y, &z)
#define sqr(x) (x) * (x)

const int maxn = 50000 + 5;
int a[maxn], n;
set<int> res;

bool judge(int y) {
    set<int> ans;
    for (int i = 0; i < n; i++) {
        int x = a[i];
        if (x < y) ans.insert(x);
        else {
            bool flag = false;
            while (x > y) x /= 2;
            while (x) {
                if (!ans.count(x)) {
                    ans.insert(x);
                    flag = true;
                    break;
                } else {
                    x /= 2;
                }
            }
            if (!flag) return false;
        }
    }
    res.clear();
    for (set<int>::iterator it = ans.begin(); it != ans.end(); it++) res.insert(*it);
    return true;
}

int main() {
    scan(n);
    for (int i = 0; i < n; i++) scan(a[i]);
    sort(a, a + n);
    int L = 0, R = 1e9, M = (L + R) >> 1;
    while (L < R) {
        if (R == L + 1) {
            if (judge(L)) M = L;
            else {
                judge(R);
                M = R;
            }
            break;
        } else {
            M = (L + R) >> 1;
            if (judge(M)) R = M;
            else L = M;
        }
    }
    for (set<int>::iterator it = res.begin(); it != res.end(); it++) printf("%d ", *it);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值