Codeforces Round #412 (rated, Div. 2, base on VK Cup 2017 Round 3) E. Prairie Partition 二分+贪心

E. Prairie Partition

It can be shown that any positive integer x can be uniquely represented as x = 1 + 2 + 4 + ... + 2k - 1 + r, where k and r are integers, k ≥ 0, 0 < r ≤ 2k. Let‘s call that representation prairie partition of x.

For example, the prairie partitions of 12, 17, 7 and 1 are:

12 = 1 + 2 + 4 + 5,

17 = 1 + 2 + 4 + 8 + 2,

7 = 1 + 2 + 4,

1 = 1.

Alice took a sequence of positive integers (possibly with repeating elements), replaced every element with the sequence of summands in its prairie partition, arranged the resulting numbers in non-decreasing order and gave them to Borys. Now Borys wonders how many elements Alice‘s original sequence could contain. Find all possible options!

Input

The first line contains a single integer n (1 ≤ n ≤ 105) — the number of numbers given from Alice to Borys.

The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 1012; a1 ≤ a2 ≤ ... ≤ an) — the numbers given from Alice to Borys.

Output

Output, in increasing order, all possible values of m such that there exists a sequence of positive integers of length m such that if you replace every element with the summands in its prairie partition and arrange the resulting numbers in non-decreasing order, you will get the sequence given in the input.

If there are no such values of m, output a single integer -1.

Examples

input

81 1 2 2 3 4 5 8

output

2 

Note

In the first example, Alice could get the input sequence from [6, 20] as the original sequence.

In the second example, Alice‘s original sequence could be either [4, 5] or [3, 3, 3].

 题意:

  每个数都可以表示成2的连续次方和加上一个r

  例如:12 = 1 + 2 + 4 + 5,

  17 = 1 + 2 + 4 + 8 + 2,

  现在给你这些数,让你反过来组成12,17,但是是有不同方案的

  看看样列就懂了,问你方案的长度种类

题解:

   将所有连续的2^x,处理出来,假设有now个序列

   最后剩下的数,我们必须将其放到上面now的尾端,但是我们优先放与当前值最接近的序列尾端,以防大一些的数仍然有位置可以放

   处理出满足条件最多序列数

  二分最少的能满足条件的序列数,也就是将mid个序列全部插入到上面now-mid个序列尾端,这里贪心选择2^x,x小的

#include<bits/stdc++.h>
using namespace std;
#pragma comment(linker, "/STACK:102400000,102400000")
#define ls i<<1
#define rs ls | 1
#define mid ((ll+rr)>>1)
#define pii pair<int,int>
#define MP make_pair
typedef long long LL;
const long long INF = 1e18+1LL;
const double Pi = acos(-1.0);
const int N = 1e5+10, M = 1e3+20, mod = 1e9+7,inf = 2e9;

LL H[70],a[N];
int No,cnt[N],n,cnts;
vector<LL > G,ans;
vector<LL > all[N];
int sum[N],sum2[N];
pair<int,LL> P[N];

void go(LL x) {
    int i;
    for(i = 0; i <= 60; ++i) {
        if(x < H[i])
            break;
    }
    i--;
    for(int j = 0; j <= i; ++j) {
        cnt[j]--;
        if(cnt[j] < 0) No = 1;
        return ;
    }
}
int cango(LL x) {
    if(x == 0) return 0;
    int ok = 0;
    for(int i = 0; i <= 60; ++i) {
        if(H[i] <= x) {
            cnt[i]--;
            if(cnt[i] < 0) {
                ok = 1;
            }
        }
    }
    if(ok) {
       for(int i = 0; i <= 60; ++i)
            if(H[i] <= x) cnt[i]++;
        return 0;
    }
    else return 1;
}
int can(LL now) {
    for(int i = G.size()-1; i >= 0; --i) {
        int ok = 0;
        for(int j = 1; j <= 60; ++j) {
            if(G[i] <= H[j] && sum[j-1]) {
                sum[j-1]--;
                P[++cnts] = MP(j-1,G[i]);
                ok = 1;
                G.pop_back();
                break;
            }
        }
        if(!ok) return 0;
    }
    return 1;
}
int allcan(int x) {
    int j = x+1,i = 0;
    int ok;
    while(j <= cnts && i < G.size()) {
        if(P[j].second  != 0) j++;
        else if(H[P[j].first+1] < G[i]) j++;
        else i++,j++;
    }
    if(i == G.size()) {
        return 1;
    }
    else return 0;
}
int check(int x) {
    x = cnts - x;
    if(x > cnts) return 0;
    if(x == 0) return 1;
    G.clear();
    for(int i = 1; i <= x; i++) {
        for(int j = 0; j <= P[i].first; ++j) {
            G.push_back(H[j]);
        }
        if(P[i].second) {
            G.push_back(P[i].second);
        }
    }
    //for(int i = 0; i < G.size(); ++i) cout<<G[i]<<" ";cout<<endl;
    if(allcan(x)) {
        return 1;
    }
    else return 0;
}
int main() {
    H[0] = 1;
    for(int i = 1; i <= 60; ++i)H[i] = H[i-1]*2LL;
    scanf("%d",&n);
    for(int i = 1; i <= n; ++i) {
        scanf("%I64d",&a[i]);
        int ok = 0;
        for(int j = 0; j <= 60; ++j) {
            if(a[i] == H[j]) {
                ok = 1;
                cnt[j]++;
                break;
            }
        }
        if(!ok) G.push_back(a[i]);
    }
    int now = 0;
    for(int i = 60; i >=0; --i) {
        while(cnt[i]) {
            if(cango(H[i])) {
             now++;
             sum[i]++;
            }
            else break;
        }
    }
    for(int i = 0; i <= 60; ++i)
        for(int j = 1; j <= cnt[i]; ++j) G.push_back(H[i]);
    int l= 1,r,ans = -1,tmpr;
    if(can(now)) r = now;
    else r = -1;
    tmpr = r;
    for(int i = 0; i <= 60; ++i) {
        for(int j = 1; j <= sum[i]; ++j) {
            P[++cnts] = MP(i,0);
        }
    }
    sort(P+1,P+cnts+1);
    while(l <= r) {
        int md = (l + r) >> 1;
        if(check(md)) {
            ans = md;
            r = md-1;
        }
        else l = md+1;
    }
    //cout<<ans<<endl;
    if(tmpr == -1) puts("-1");
    else {
        for(int i = ans; i <= tmpr; ++i) cout<<i<<" ";
        cout<<endl;
    }
    return 0;
}
/*
5
1 2 3 4 5
*/
时间: 2024-08-02 11:04:53

Codeforces Round #412 (rated, Div. 2, base on VK Cup 2017 Round 3) E. Prairie Partition 二分+贪心的相关文章

Codeforces Round #412 (rated, Div. 2, base on VK Cup 2017 Round 3) B. T-Shirt Hunt

B. T-Shirt Hunt time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard output Not so long ago the Codecraft-17 contest was held on Codeforces. The top 25 participants, and additionally random 25 participant

【树形dp】Codeforces Round #405 (rated, Div. 1, based on VK Cup 2017 Round 1) B. Bear and Tree Jumps

我们要统计的答案是sigma([L/K]),L为路径的长度,中括号表示上取整. [L/K]化简一下就是(L+f(L,K))/K,f(L,K)表示长度为L的路径要想达到K的整数倍,还要加上多少. 于是,我们现在只需要统计sigma((L+f(L,K))),最后除以K即可. 统计sigma(L)时,我们考虑计算每条边出现在了几条路径中,设u为edgei的子节点,那么这条边对答案的贡献就是siz(u)*(n-siz(u)),siz(u)为u的子树大小. 统计sigma(f(L,K))时,我们需要dp出

Codeforces Round #405 (rated, Div. 2, based on VK Cup 2017 Round 1) B

Description Bear Limak examines a social network. Its main functionality is that two members can become friends (then they can talk with each other and share funny pictures). There are n members, numbered 1 through n. m pairs of members are friends.

Codeforces Round #405 (rated, Div. 2, based on VK Cup 2017 Round 1) C

Description In the army, it isn't easy to form a group of soldiers that will be effective on the battlefield. The communication is crucial and thus no two soldiers should share a name (what would happen if they got an order that Bob is a scouter, if

Codeforces Round #405 (rated, Div. 2, based on VK Cup 2017 Round 1) A

Description Bear Limak wants to become the largest of bears, or at least to become larger than his brother Bob. Right now, Limak and Bob weigh a and b respectively. It's guaranteed that Limak's weight is smaller than or equal to his brother's weight.

Codeforces Round #470 (rated, Div. 2, based on VK Cup 2018 Round 1)C. Producing Snow+差分标记

题目链接:C. Producing Snow 题意:给两个数组v[N],T[N],v[i]表示第i天造的雪,T[i],表示第i天的温度,一堆雪如果<=T[i],当天就会融完,否则融化T[i],要求输出每天的融雪总量. 题解:我对T数组求个前缀和,就可以二分找到每堆雪在那一天(pos)融化,余下的要加进答案中ans[i],然后用一个an数组在a[i]+1,a[pos]-1,最后求再求一次前缀和. ans[i]再加上an[i]*t[i].每次操作二分logn,N次操作.复杂度O(nlogn) #in

Codeforces Round #472 (rated, Div. 2, based on VK Cup 2018 Round 2)

A. Tritonic Iridescence 题解:分类讨论.注意题目要求,至少有两种方案. 1 #pragma warning(disable:4996) 2 #include<cstdio> 3 #include<string> 4 #include<cstring> 5 #include<iostream> 6 #include<algorithm> 7 using namespace std; 8 9 int n, m; 10 stri

Codeforces Round #477 (rated, Div. 2, based on VK Cup 2018 Round 3) C. Stairs and Elevators【二分查找】

In the year of 30XX30XX participants of some world programming championship live in a single large hotel. The hotel has nn floors. Each floor has mm sections with a single corridor connecting all of them. The sections are enumerated from 11 to mm alo

【枚举】【二分】【推导】Codeforces Round #477 (rated, Div. 2, based on VK Cup 2018 Round 3) D. Resource Distribution

题意:有两个服务要求被满足,服务S1要求x1数量的资源,S2要求x2数量的资源.有n个服务器来提供资源,第i台能提供a[i]的资源.当你选择一定数量的服务器来为某个服务提供资源后,资源需求会等量地分担给它们,要求每台服务器承担的资源需求不超过其所能提供的资源需求.给定一种合法的方案,每台服务器要么没有被分配给任何一个服务,或者被分配给其中一个服务. 对服务器按能提供的资源从小到大排序.枚举给S1分配的服务器数量i,然后在a数组中二分,就可以得到给S1提供的是哪i台服务器,它们占据了a数组中连续的