結果

問題 No.3062 Rotate and Maximize
ユーザー 👑 rin204
提出日時 2025-02-26 21:06:18
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 39 ms / 2,000 ms
コード長 1,468 bytes
コンパイル時間 3,557 ms
コンパイル使用メモリ 279,776 KB
実行使用メモリ 7,324 KB
最終ジャッジ日時 2025-03-12 00:52:08
合計ジャッジ時間 5,445 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 51
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "atcoder/modint.hpp"
#include "bits/stdc++.h"
using mint = atcoder::modint998244353;

int main() {
    int n;
    std::cin >> n;
    assert(1 <= n and n <= 3000);
    std::vector<int> A(n);
    for (int i = 0; i < n; i++) {
        std::cin >> A[i];
        assert(1 <= A[i] and A[i] <= n);
        A[i]--;
    }

    std::vector<int> n_pos;
    for (int i = 0; i < n; i++) {
        if (A[i] == n - 1) n_pos.push_back(i);
    }
    if (n_pos.empty()) {
        std::cout << 0 << std::endl;
        return 0;
    }

    std::vector<int> mi(n, n);
    std::vector<int> mi_cnt(n, 0);
    std::vector<int> ac(n, 0);
    for (int i = 0; i < n; i++) {
        int a = A[i];
        ac[a]++;
        for (auto d : n_pos) {
            int p = (i + n - d) % n;
            if (a < mi[p]) {
                mi[p]     = a;
                mi_cnt[p] = 1;
            } else if (a == mi[p]) {
                mi_cnt[p]++;
            }
        }
    }

    std::vector<int> cnt(n, 0);
    std::vector<int> cnt2(n, 0);
    for (int i = 0; i < n; i++) {
        cnt[mi[i]]++;
        if (mi_cnt[i] == ac[mi[i]]) {
            cnt2[mi[i]]++;
        }
    }

    mint ans = 1;
    int tot  = 0;
    for (int i = n - 1; i >= 0; i--) {
        tot += cnt[i];
        if (i == n - 1) {
        } else if (ac[i] > 0) {
            ans *= cnt2[i];
        } else {
            ans *= tot;
        }
        tot--;
    }

    ans *= n;
    std::cout << ans.val() << std::endl;
}
0