码迷,mamicode.com
首页 > 其他好文 > 详细

CodeForces-916B-Jamie and Binary Sequence(changed after round)(构造)

时间:2019-08-22 01:18:20      阅读:131      评论:0      收藏:0      [点我收藏+]

标签:power   tps   cin   没有   main   output   define   change   bin   

链接:

https://vjudge.net/problem/CodeForces-916B

题意:

Jamie is preparing a Codeforces round. He has got an idea for a problem, but does not know how to solve it. Help him write a solution to the following problem:

Find k integers such that the sum of two to the power of each number equals to the number n and the largest integer in the answer is as small as possible. As there may be multiple answers, you are asked to output the lexicographically largest one.

To be more clear, consider all integer sequence with length k (a1,?a2,?...,?ak) with . Give a value to each sequence. Among all sequence(s) that have the minimum y value, output the one that is the lexicographically largest.

For definitions of powers and lexicographical order see notes.

思路:

先构造出最少数量的序列,如果此时长度大于m,就是没有可能.
否则优先判断前面较大的能不能全部用掉,否则从小的开始去减.

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
//#include <memory.h>
#include <queue>
#include <set>
#include <map>
#include <algorithm>
#include <math.h>
#include <stack>
#include <string>
#include <assert.h>
#include <iomanip>
#define MINF 0x3f3f3f3f
using namespace std;
typedef long long LL;

LL n, m;

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin >> n >> m;
    multiset<int> st;
    for (int i = 63;i >= 0;i--)
    {
        if ((1ULL<<i) <= n)
        {
            st.insert(i);
            n -= (1LL<<i);
        }
    }
    if (st.size() > m)
        puts("No");
    else
    {
        while (st.size() < m)
        {
            if (st.size() == 1)
            {
                int t = *st.rbegin();
                st.erase(*st.rbegin());
                st.insert(t-1);
                st.insert(t-1);
            }
            else
            {
                int cnt = st.count(*st.rbegin());
                if (m-st.size() >= cnt)
                {
                    int t = *st.rbegin();
                    st.erase(t);
                    for (int i = 1;i <= cnt*2;i++)
                        st.insert(t-1);
                }
                else
                {
                    int t = *st.begin();
                    t--;
                    st.erase(st.begin());
                    while (st.size()+2 < m)
                    {
                        st.insert(t);
                        t--;
                    }
                    st.insert(t);
                    st.insert(t);
                }
            }
        }
        cout << "Yes" << endl;
        for (multiset<int>::reverse_iterator it = st.rbegin();it != st.rend();++it)
            cout << *it << ' ';
        cout << endl;
    }

    return 0;
}

CodeForces-916B-Jamie and Binary Sequence(changed after round)(构造)

标签:power   tps   cin   没有   main   output   define   change   bin   

原文地址:https://www.cnblogs.com/YDDDD/p/11391963.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!