提出 #69531647


ソースコード 拡げる

#include <bits/stdc++.h>
#ifdef LOCAL
#include <debug.hpp>
#else
#define debug(...) void(0)
#endif

template <class T, class U = T> bool chmin(T& x, U&& y) { return y < x and (x = std::forward<U>(y), true); }

template <class T, class U = T> bool chmax(T& x, U&& y) { return x < y and (x = std::forward<U>(y), true); }

template <class T> void mkuni(std::vector<T>& v) {
    std::ranges::sort(v);
    auto result = std::ranges::unique(v);
    v.erase(result.begin(), result.end());
}

template <class T> int lwb(const std::vector<T>& v, const T& x) {
    return std::distance(v.begin(), std::ranges::lower_bound(v, x));
}

#include <atcoder/modint>

using namespace std;

using ll = long long;

using mint = atcoder::modint998244353;

void solve() {
    int N;
    cin >> N;
    vector<int> A(N);
    for (int i = 0; i < N; i++) {
        cin >> A[i];
        if (A[i] != -1) {
            A[i]--;
        }
    }

    int L = -1, R = -1;
    for (int i = 0; i < N; i++) {
        if (A[i] == -1) {
            continue;
        }
        if (A[i] != i + 1 and A[i] != i - 1) {
            if (L != -1) {
                cout << 0 << "\n";
                return;
            }
            L = min(i, A[i]);
            R = max(i, A[i]);
        }
    }
    auto right = [&](int i) { return A[i] == -1 or A[i] == i + 1; };
    auto left = [&](int i) { return A[i] == -1 or A[i] == i - 1; };
    if (L != -1) {  // 長さ 2 以上のサイクルが既に存在
        for (int i = 0; i < L; i++) {
            if (not right(i)) {
                cout << 0 << "\n";
                return;
            }
        }
        for (int i = R + 1; i < N; i++) {
            if (not left(i)) {
                cout << 0 << "\n";
                return;
            }
        }
        if (A[L] == R) {
            for (int i = L + 1; i <= R; i++) {
                if (not left(i)) {
                    cout << 0 << "\n";
                    return;
                }
            }
        } else {
            assert(A[R] == L);
            for (int i = L; i < R; i++) {
                if (not right(i)) {
                    cout << 0 << "\n";
                    return;
                }
            }
        }
        cout << 1 << "\n";
        return;
    }

    int Right = 0, Left = 0;
    while (Right < N and right(Right)) {
        Right++;
    }
    while (Left < N and left(N - 1 - Left)) {
        Left++;
    }
    mint ans = 0;
    for (int i = 0; i < N; i++) {
        if (Right < i or Left < N - 1 - i) {
            continue;
        }
        if (A[i] == -1) {
            ans += N - (i != 0);
        } else {
            if (A[i] != i - 1) {
                ans++;
            }
        }
    }

    cout << ans.val() << "\n";
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout << fixed << setprecision(15);

    solve();
    return 0;
}

提出情報

提出日時
問題 C - Tree Sequence
ユーザ rniya
言語 C++ 23 (gcc 12.2)
得点 600
コード長 3005 Byte
結果 AC
実行時間 12 ms
メモリ 3872 KiB

ジャッジ結果

セット名 Sample All
得点 / 配点 0 / 0 600 / 600
結果
AC × 3
AC × 34
セット名 テストケース
Sample 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt
All 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt, 01_random_00.txt, 01_random_01.txt, 01_random_02.txt, 01_random_03.txt, 01_random_04.txt, 01_random_05.txt, 01_random_06.txt, 01_random_07.txt, 01_random_08.txt, 01_random_09.txt, 01_random_10.txt, 01_random_11.txt, 01_random_12.txt, 01_random_13.txt, 01_random_14.txt, 01_random_15.txt, 01_random_16.txt, 01_random_17.txt, 01_random_18.txt, 01_random_19.txt, 02_min_00.txt, 02_min_01.txt, 02_min_02.txt, 02_min_03.txt, 02_min_04.txt, 02_min_05.txt, 02_min_06.txt, 02_min_07.txt, 02_min_08.txt, 02_min_09.txt, 02_min_10.txt
ケース名 結果 実行時間 メモリ
00_sample_00.txt AC 1 ms 3380 KiB
00_sample_01.txt AC 1 ms 3420 KiB
00_sample_02.txt AC 1 ms 3472 KiB
01_random_00.txt AC 9 ms 3524 KiB
01_random_01.txt AC 7 ms 3540 KiB
01_random_02.txt AC 12 ms 3780 KiB
01_random_03.txt AC 11 ms 3800 KiB
01_random_04.txt AC 11 ms 3776 KiB
01_random_05.txt AC 8 ms 3792 KiB
01_random_06.txt AC 7 ms 3500 KiB
01_random_07.txt AC 9 ms 3788 KiB
01_random_08.txt AC 10 ms 3772 KiB
01_random_09.txt AC 11 ms 3832 KiB
01_random_10.txt AC 8 ms 3872 KiB
01_random_11.txt AC 8 ms 3796 KiB
01_random_12.txt AC 8 ms 3828 KiB
01_random_13.txt AC 8 ms 3792 KiB
01_random_14.txt AC 7 ms 3772 KiB
01_random_15.txt AC 7 ms 3784 KiB
01_random_16.txt AC 7 ms 3800 KiB
01_random_17.txt AC 8 ms 3796 KiB
01_random_18.txt AC 7 ms 3872 KiB
01_random_19.txt AC 7 ms 3836 KiB
02_min_00.txt AC 1 ms 3452 KiB
02_min_01.txt AC 1 ms 3396 KiB
02_min_02.txt AC 1 ms 3516 KiB
02_min_03.txt AC 1 ms 3280 KiB
02_min_04.txt AC 1 ms 3412 KiB
02_min_05.txt AC 1 ms 3516 KiB
02_min_06.txt AC 1 ms 3472 KiB
02_min_07.txt AC 1 ms 3464 KiB
02_min_08.txt AC 1 ms 3528 KiB
02_min_09.txt AC 1 ms 3416 KiB
02_min_10.txt AC 1 ms 3388 KiB