2024.12.29 周日

2024.12.29 周日


Q1. 1100

You are given a number in binary representation consisting of exactly n bits, possibly, with leading zeroes. For example, for n=5 the number 6 will be given as 00110, and for n=4 the number 9 will be given as 1001.

Let's fix some integer i such that 1in. In one operation you can swap any two adjacent bits in the binary representation. Your goal is to find the smallest number of operations you are required to perform to make the number divisible by 2i, or say that it is impossible.

Please note that for each 1in you are solving the problem independently.


------------------------独自思考分割线------------------------

  • 递推思想。 对于每个答案单独快速计算太难,考虑递推。


A1.

  1. 题意:给定一个01串代表的二进制的正整数。每次操作可交换相邻字母。问对于每一个 i[1,n] ,输出最小的操作次数使得原字符串对应的二进制数是 2i 的倍数。
  2. 对于每个答案单独快速计算太难,考虑递推。
  3. 最优操作次数就是将 1 与高位最近的 0 交换。本质就是快速找到高位最近的 0。
  4. 考虑指针维护。wa2发是因为代码没有保证高位。
  5. 维护有2种方法,1好理解,可以证明2也是对的。

------------------------代码分割线------------------------

A1.

#include <bits/stdc++.h>
#define int long long //
#define endl '\n'     // 交互/调试 关
using namespace std;
#define bug(BUG) cout << "bug:# " << (BUG) << endl
#define bug2(BUG1, BUG2) cout << "bug:# " << (BUG1) << " " << (BUG2) << endl
#define bug3(BUG1, BUG2, BUG3) cout << "bug:# " << (BUG1) << ' ' << (BUG2) << ' ' << (BUG3) << endl
void _();
signed main()
{
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    cout << fixed << setprecision(6);
    int T = 1;
    cin >> T;
    while (T--)
        _();
    return 0;
}

void _()
{
    int n;
    cin >> n;
    string s;
    cin >> s;
    reverse(s.begin(), s.end());
    s = ' ' + s + ' ';
    int f = 0, id = 2;
    auto find = [&](int st)
    {
        while (1)
        {
            for (; id <= n && s[id] == '1'; id++)
                ;
            if (id > st)
                break;
            else
                id++;
        }
    };
    for (int i = 1; i <= n; i++)
    {
        if (s[i] - '0')
        {
            find(i);
            if (id > n)
                f = -1;
            else
            {
                // bug2(i, id);
                // bug(s);
                swap(s[i], s[id]);
                f += id - i;
            }
        }
        cout << f << ' ';
    }
    cout << endl;
}

A1

#include <bits/stdc++.h>
#define int long long //
#define endl '\n'     // 交互/调试 关
using namespace std;
#define bug(BUG) cout << "bug:# " << (BUG) << endl
#define bug2(BUG1, BUG2) cout << "bug:# " << (BUG1) << " " << (BUG2) << endl
#define bug3(BUG1, BUG2, BUG3) cout << "bug:# " << (BUG1) << ' ' << (BUG2) << ' ' << (BUG3) << endl
void _();
signed main()
{
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    cout << fixed << setprecision(6);
    int T = 1;
    cin >> T;
    while (T--)
        _();
    return 0;
}

void _()
{
    int n;
    cin >> n;
    string s;
    cin >> s;
    reverse(s.begin(), s.end());
    s = '0' + s;
    int res = 0;
    int idx = 1;
    bool ok = 1;
    vector<int> f(n + 1);
    for (int i = 1; i <= n; i++, idx++)
        if (!ok)
            f[i] = -1;
        else
        {
            if (s[i] == '0')
                f[i] = f[i - 1];
            else
            {
                for (; idx <= n && s[idx] == '1'; idx++)
                    ;
                if (idx > n)
                {
                    ok = 0;
                    f[i] = -1;
                }
                else
                {
                    // bug2(i, idx);
                    f[i] = f[i - 1] + idx - i;
                    swap(s[i], s[idx]);
                }
            }
        }
    for (int i = 1; i <= n; i++)
        cout << f[i] << ' ';
    cout << endl;
}

---

posted @   Jkke  阅读(4)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
点击右上角即可分享
微信分享提示