結果

問題 No.1867 Partitions and Inversions
ユーザー CyanmondCyanmond
提出日時 2022-03-06 10:10:39
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,787 bytes
コンパイル時間 2,375 ms
コンパイル使用メモリ 210,296 KB
実行使用メモリ 71,200 KB
最終ジャッジ日時 2023-09-28 00:25:22
合計ジャッジ時間 17,989 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 9 ms
37,040 KB
testcase_01 AC 8 ms
38,940 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 AC 9 ms
36,968 KB
testcase_33 AC 9 ms
39,008 KB
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
testcase_37 AC 9 ms
38,940 KB
testcase_38 WA -
testcase_39 WA -
testcase_40 WA -
testcase_41 WA -
testcase_42 WA -
testcase_43 WA -
testcase_44 WA -
testcase_45 WA -
testcase_46 WA -
testcase_47 WA -
testcase_48 WA -
testcase_49 WA -
testcase_50 WA -
testcase_51 WA -
testcase_52 WA -
testcase_53 WA -
testcase_54 WA -
testcase_55 WA -
testcase_56 WA -
testcase_57 WA -
testcase_58 WA -
testcase_59 WA -
testcase_60 WA -
testcase_61 WA -
testcase_62 WA -
testcase_63 AC 193 ms
68,920 KB
testcase_64 AC 9 ms
38,964 KB
testcase_65 AC 8 ms
37,044 KB
testcase_66 AC 9 ms
39,000 KB
testcase_67 AC 238 ms
64,796 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#pragma GCC target("avx2")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
using namespace std;

int cnt[3001][3001], dp[3001][3001];

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int N;
    cin >> N;
    vector<int> P(N);
    for (int i = 0; i < N; i++) {
        cin >> P[i];
    }
    for (int i = 0; i < N; i++) {
        for (int j = i; j < N; j++) {
            if (P[i] > P[j]) {
                cnt[i][j]++;
                cnt[j][j]--;
            }
        }
    }
    for (int j = 0; j < N; j++) {
        for (int i = 0; i < j; i++) {
            cnt[i + 1][j] += cnt[i][j];
        }
    }
    for (int i = 0; i < N; i++) {
        for (int j = i; j < N; j++) {
            cnt[i][j + 1] += cnt[i][j];
        }
    }

    for (int i = 0; i < 3001; ++i) dp[i][1] = 0;
    for (int i = 2; i <= N; i++) {
        stack<int> st;
        st.push(i - 2);

        auto is_better = [&](int a, int b, int c) {
            return dp[a][i - 1] + cnt[a][c] < dp[b][i - 1] + cnt[b][c];
        };

        for (int j = i - 1; j < N; j++) {
            while (st.size() > 1) {
                int t = st.top();
                st.pop();
                int b = st.top();
                if (is_better(t, b, j)) {
                    st.push(t);
                    break;
                }
            }
            dp[j][i] = dp[st.top()][i - 1] + cnt[st.top()][j];

            while (not st.empty()) {
                int t = st.top();
                if (is_better(j, t, N - 1))
                    st.pop();
                else
                    break;
            }

            st.push(j);
        }
    }
    for (int i = 1; i <= N; i++) {
        cout << dp[N - 1][i] << '\n';
    }
}
// 正当性なし
0