跳到正文

题解

题解:CF1458D Flip and Reverse

Priestess_SLG约 2 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 2 分钟
题目
CF1458D

版本

  1. v1当前

这是给人想的????????????

构建图模型,把 00 看做是 x→x−1x\to x-1 的有向边,11 看做是 x→x+1x\to x+1 的有向边,这样一次操作的合法条件就是在图上走一个环,答案就是这张图字典序最小的欧拉路径。然后贪心处理,对于一个点 uu 如果能走到 u−1u-1 且若 u+1u+1 有边且能走回来,那么就贪心的走 u−1u-1,否则走 u+1u+1。时间复杂度为 O(n)O(n)。

代码经过了一点压行处理。

C++
#pragma GCC optimize(3, "Ofast", "inline", "unroll-loops")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#define int long long
using namespace std;

const int N = 1000010;
const int inf = 1e18;
const int mod = 1e9 + 7;

char s[N];
int buc[N];

signed main()
{
    int T;
    cin >> T;
    while (T--)
    {
        scanf("%s", s + 1);
        int n = strlen(s + 1), x = n;
        fill(buc, buc + n + n + 2, 0), for_each(s + 1, s + n + 1, [&](char &o) { ++buc[(o == '1') ? (x++) : (--x)]; });
        for_each(s + 1, s + (x = n) + 1, [&](...) { cout << ((buc[x - 1] && (buc[x - 1] > 1 || !buc[x])) ? (--buc[--x], 0) : (--buc[x++], 1)); });
        cout << '\n';
    }
    return 0;
}
原文