zoukankan      html  css  js  c++  java
  • CodeForces-916B-Jamie and Binary Sequence(changed after round)(构造)

    链接:

    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;
    }
    
  • 相关阅读:
    Spring 实例化bean的三种方式
    Mybatis和Hibernate比较
    MyBatis学习总结(一)——MyBatis快速入门
    Java EE的十三个规范
    Python 测试代码覆盖率统计工具 coverage.py
    mysql explain执行计划详解
    Django模型的Field Types
    使程序在Linux下后台运行,程序运行前后台切换
    ubuntu中将本地文件上传到服务器
    Python-内置函数小结
  • 原文地址:https://www.cnblogs.com/YDDDD/p/11391963.html
Copyright © 2011-2022 走看看