結果

問題 No.1827 最長部分スーパーリッチ門松列列
ユーザー suisen
提出日時 2022-01-29 00:35:23
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 64 ms / 2,000 ms
コード長 1,122 bytes
コンパイル時間 802 ms
コンパイル使用メモリ 73,576 KB
最終ジャッジ日時 2025-01-27 17:31:59
ジャッジサーバーID
(参考情報)
judge3 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 24
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>

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

    int t;
    std::cin >> t;
    while (t --> 0) {
        int n;
        std::cin >> n;
        std::vector<int> p(n);
        for (auto &e : p) {
            std::cin >> e;
            --e;
        }
        std::vector<int> q(n);
        for (int i = 0; i < n; ++i) {
            q[p[i]] = i;
        }
        int block_num = 1;
        std::vector<int> x(n, 1);
        auto update = [&](int i, int v) {
            block_num -= i > 0     and x[i - 1] != x[i];
            block_num -= i + 1 < n and x[i + 1] != x[i];
            x[i] = v;
            block_num += i > 0     and x[i - 1] != x[i];
            block_num += i + 1 < n and x[i + 1] != x[i];
        };

        int ans = 0;
        for (int i : q) {
            update(i, -1);
            int len = block_num;
            len -= i > 0     and x[i - 1] == 1;
            len -= i + 1 < n and x[i + 1] == 1;
            ans = std::max(ans, len);
            update(i, 0);
        }
        std::cout << ans << '\n';
    }
    return 0;
}
0