社区讨论

RE #7 求助

P10032「Cfz Round 3」Mex of Sequence参与者 1已保存回复 0

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
0 条
当前快照
1 份
快照标识符
@mkqmqch8
此快照首次捕获于
2026/01/23 16:39
4 周前
此快照最后确认于
2026/01/23 21:56
4 周前
查看原帖
CPP
// #define IO_FILE_NAME ""
// #define LOCAL_FILE_IO
#define MANY_TESTS 1

#ifdef LOCAL
#include "all.hh"
#else
#include <bits/stdc++.h>
#define debug(...) (void)(89'117'122'117)
#define debug_if(...) (void)(89'117'122'117)
#define verify(...) (void)(__VA_ARGS__)
#define FOR(i, a, b) for (ui i = a; i <= (b); i++)
#define REP(i, a, b) for (ui i = a; i < (b); i++)
#define RFOR(i, a, b) for (ui i = (a) + 1; i-- > (b);)
#define RREP(i, a, b) for (ui i = a; i > (b); i--)

using uc = unsigned char;
using ui = unsigned int;
using i64 = long long;
using u64 = unsigned long long;
using fp = double;
using std::ptrdiff_t;
using std::size_t;
#if __cplusplus >= 202002L
namespace ranges = std::ranges;
namespace views = std::views;
#endif

namespace {
#ifdef IO_FILE_NAME
    std::ifstream fin(IO_FILE_NAME ".in");
    std::ofstream fout(IO_FILE_NAME ".out");
#else
    std::istream& fin = std::cin;
    std::ostream& fout = std::cout;
#endif // IO_FILE_NAME

    constexpr ui INF32 = ui(1e9);
    constexpr u64 INF64 = u64(1e18);

    struct CkminFn {
        template <typename T, typename U>
        constexpr bool operator()(T& a, const U& b) const {
            return b < a && (a = b, true);
        }
    };
    constexpr CkminFn ckmin{};

    struct CkmaxFn {
        template <typename T, typename U>
        constexpr bool operator()(T& a, const U& b) const {
            return b > a && (a = b, true);
        }
    };
    constexpr CkmaxFn ckmax{};

    template <typename F> struct YcFn {
        F f_;
        constexpr YcFn(F&& f) : f_(std::forward<F>(f)) {}
        template <typename... A> constexpr decltype(auto) operator()(A&&... a) {
            return f_(*this, std::forward<A>(a)...);
        }
    };
    template <typename T> constexpr auto yc(T&& f) {
        return YcFn<std::decay_t<T>>(std::forward<T>(f));
    }

    template <typename F, typename T>
    constexpr auto do_with(T&& v_, F&& f_ = {}) {
        return [v = std::forward<T>(v_),
                f = std::forward<F>(f_)](auto&& u) -> decltype(auto) {
            return f(std::forward<decltype(u)>(u), v);
        };
    }
    template <typename F, typename T>
    constexpr auto assign_with(T&& v_, F&& f_ = {}) {
        return [v = std::forward<T>(v_),
                f = std::forward<F>(f_)](auto&& u) -> decltype(auto) {
            return u = f(std::move(u), v);
        };
    }

    struct MinFn {
        template <typename T>
        constexpr T operator()(const T& a, const T& b) const {
            return a < b ? a : b;
        }
        template <typename T, typename... Args>
            requires(sizeof...(Args) > 1)
        constexpr T operator()(const T& a, const Args&... b) const {
            return (*this)(a, (*this)(b...));
        }
    };
    constexpr MinFn min{};

    struct MaxFn {
        template <typename T>
        constexpr T operator()(const T& a, const T& b) const {
            return a > b ? a : b;
        }
        template <typename T, typename... Args>
            requires(sizeof...(Args) > 1)
        constexpr T operator()(const T& a, const Args&... b) const {
            return (*this)(a, (*this)(b...));
        }
    };
    constexpr MaxFn max{};

    struct GcdFn {
        template <std::integral T> constexpr T operator()(T a, T b) const {
            while (b) {
                const T tmp = a;
                a = b;
                b = tmp % b;
            }
            return a;
        }
        template <std::integral T, typename... Args>
            requires(sizeof...(Args) > 1)
        constexpr T operator()(const T& a, const Args&... b) const {
            return (*this)(a, (*this)(b...));
        }
    };
    constexpr GcdFn gcd{};

    struct LcmFn {
        template <std::integral T> constexpr T operator()(T a, T b) const {
            return a / gcd(a, b) * b;
        }
        template <std::integral T, typename... Args>
            requires(sizeof...(Args) > 1)
        constexpr T operator()(T a, Args... b) const {
            return (*this)(a, (*this)(b...));
        }
    };
    constexpr LcmFn lcm{};
} // namespace
#endif // LOCAL
// fastio begin.
#define HAS_FAST_IO
#include <cctype>
#include <cstddef>
#include <iostream>
#include <string>
#include <type_traits>
#include <utility>

namespace fast_input {
    constexpr ui sz = 1u << 21;
    bool sgn;
    char buf[sz], *b = buf, *e = buf;
    uc c;
    inline void getch() {
        if (b == e) {
            fin.read(buf, sz);
            if (fin.gcount() == 0) {
                c = ' ';
                return;
            }
            b = buf;
            e = buf + fin.gcount();
        }
        c = uc(*b++);
    }
    inline void read(char& r) {
        getch();
        while (c == ' ' || c == '\n' || c == '\r')
            getch();
        r = char(c);
    }
    inline void read(bool& r) {
        getch();
        while (!std::isdigit(c))
            getch();
        r = (c != '0');
    }
    template <std::unsigned_integral T> inline void read(T& r) {
        getch();
        while (!std::isdigit(c)) {
            getch();
        }
        r = c & 15;
        getch();
        while (std::isdigit(c)) {
            r = r * 10 + (c & 15);
            getch();
        }
    }
    template <std::signed_integral T>
        requires(!std::is_same_v<T, char>)
    inline void read(T& r) {
        sgn = false;
        static std::make_unsigned_t<T> n;
        getch();
        while (!std::isdigit(c)) {
            sgn = (c == '-');
            getch();
        }
        n = c & 15;
        getch();
        while (std::isdigit(c)) {
            n = n * 10 + (c & 15);
            getch();
        }
        r = sgn ? -n : n;
    }
    inline void read(std::string& s) {
        getch();
        while (c == ' ' || c == '\n' || c == '\r')
            getch();
        s = char(c);
        getch();
        while (c != ' ' && c != '\n' && c != '\r') {
            s += char(c);
            getch();
        }
    }
    template <typename... Args>
    inline std::enable_if_t<(sizeof...(Args) > 1)> read(Args&&... args) {
        (read(std::forward<Args>(args)), ...);
    }
} // namespace fast_input
namespace fast_output {
    constexpr ui sz = 1u << 21;
    char buf[sz], *b = buf, *e = buf + sz;
    uc a[40], *p = a;
    inline void flush() {
        fout.write(buf, b - buf);
        b = buf;
    }
    inline void putch(uc c) {
        if (b == e)
            flush();
        *b++ = char(c);
    }
    inline void write(char c) { putch(uc(c)); }
    template <std::unsigned_integral T> inline void write(T n) {
        if (!n) {
            putch('0');
            return;
        }
        while (n) {
            *++p = uc((n % 10) | 48);
            n /= 10;
        }
        while (p != a)
            putch(*p--);
    }
    template <std::signed_integral T>
        requires(!std::is_same_v<T, char>)
    inline void write(T n) {
        if (n < 0) {
            putch('-');
            n = -n;
        }
        write(std::make_unsigned_t<T>(n));
    }
    inline void write(const char* s) {
        while (*s)
            putch(uc(*s++));
    }
    inline void write(const std::string& s) { write(s.c_str()); }
    template <typename... Args>
        requires(sizeof...(Args) > 1)
    inline void write(Args&&... args) {
        (write(args), ...);
    }
    struct AutoFlush {
        ~AutoFlush() { flush(); }
    } auto_flush;
} // namespace fast_output
using fast_input::read;
using fast_output::write;
// fastio end.

using namespace std::literals;

namespace {
    constexpr size_t MAXN = 1'000'005;
    ui n, m, a[MAXN], buc[MAXN];

    void test_case([[maybe_unused]] ui ca) {
        read(n, m);
        std::fill_n(buc, n + 1, 0u);
        FOR(i, 1, n) {
            read(a[i]);
            if (a[i] <= n) {
                buc[a[i]]++;
            }
        }
        if (m == 1) {
            ui mex = ui(std::find(buc, buc + n + 1, 0u) - buc);
            FOR(i, 1, n) {
                if (buc[a[i]] == 1) {
                    a[i] = min(mex, a[i]);
                } else {
                    a[i] = mex;
                }
            }
        } else {
            ui base = ui(std::find_if(buc, buc + n + 1,
                                      do_with(1u, std::not_equal_to{})) -
                         buc);
            ui ge =
                std::accumulate(buc + base, buc + n + 1, 0u) +
                ui(std::count_if(a + 1, a + n + 1, do_with(n, std::greater{})));
            if (buc[base] == 0) {
                std::replace_if(a + 1, a + n + 1, do_with(base, std::greater{}),
                                m % 2 ? base : base + (ge > 1));
            } else {
                std::replace_if(a + 1, a + n + 1,
                                do_with(base, std::greater_equal{}),
                                m % 2 ? base + 1 : base);
            }
        }
        FOR(i, 1, n) { write(a[i], " \n"[i == n]); }
    }
} // namespace

int main() {
    std::cin.tie(nullptr)->sync_with_stdio(false);

    ui t = 1;
#if MANY_TESTS
#ifdef HAS_FAST_IO
    read(t);
#else
    fin >> t;
#endif // HAS_FAST_IO
#endif
    for (ui ca = 1; ca <= t; ++ca) {
        test_case(ca);
    }
}

回复

0 条回复,欢迎继续交流。

正在加载回复...