結果

問題 No.1867 Partitions and Inversions
ユーザー ぷらぷら
提出日時 2022-03-04 20:29:50
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 435 ms / 5,000 ms
コード長 2,124 bytes
コンパイル時間 2,153 ms
コンパイル使用メモリ 205,372 KB
実行使用メモリ 74,216 KB
最終ジャッジ日時 2023-09-25 22:03:39
合計ジャッジ時間 23,313 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
5,392 KB
testcase_01 AC 4 ms
7,408 KB
testcase_02 AC 420 ms
74,028 KB
testcase_03 AC 414 ms
73,996 KB
testcase_04 AC 415 ms
73,952 KB
testcase_05 AC 422 ms
74,048 KB
testcase_06 AC 422 ms
74,124 KB
testcase_07 AC 413 ms
74,000 KB
testcase_08 AC 420 ms
73,960 KB
testcase_09 AC 420 ms
74,048 KB
testcase_10 AC 416 ms
74,032 KB
testcase_11 AC 429 ms
74,052 KB
testcase_12 AC 415 ms
74,200 KB
testcase_13 AC 419 ms
74,004 KB
testcase_14 AC 425 ms
73,984 KB
testcase_15 AC 414 ms
73,960 KB
testcase_16 AC 413 ms
74,004 KB
testcase_17 AC 424 ms
74,132 KB
testcase_18 AC 419 ms
74,028 KB
testcase_19 AC 431 ms
73,980 KB
testcase_20 AC 421 ms
74,028 KB
testcase_21 AC 420 ms
73,980 KB
testcase_22 AC 426 ms
74,028 KB
testcase_23 AC 424 ms
73,980 KB
testcase_24 AC 427 ms
74,044 KB
testcase_25 AC 419 ms
74,080 KB
testcase_26 AC 424 ms
73,988 KB
testcase_27 AC 428 ms
73,960 KB
testcase_28 AC 419 ms
74,008 KB
testcase_29 AC 428 ms
74,008 KB
testcase_30 AC 435 ms
74,004 KB
testcase_31 AC 424 ms
74,216 KB
testcase_32 AC 3 ms
5,532 KB
testcase_33 AC 4 ms
7,468 KB
testcase_34 AC 4 ms
7,480 KB
testcase_35 AC 4 ms
7,696 KB
testcase_36 AC 4 ms
7,564 KB
testcase_37 AC 4 ms
7,488 KB
testcase_38 AC 12 ms
18,312 KB
testcase_39 AC 12 ms
18,364 KB
testcase_40 AC 13 ms
18,360 KB
testcase_41 AC 12 ms
18,324 KB
testcase_42 AC 12 ms
18,340 KB
testcase_43 AC 154 ms
53,168 KB
testcase_44 AC 154 ms
53,140 KB
testcase_45 AC 154 ms
53,292 KB
testcase_46 AC 156 ms
53,312 KB
testcase_47 AC 157 ms
53,192 KB
testcase_48 AC 155 ms
53,144 KB
testcase_49 AC 159 ms
53,148 KB
testcase_50 AC 159 ms
53,296 KB
testcase_51 AC 164 ms
53,192 KB
testcase_52 AC 161 ms
53,188 KB
testcase_53 AC 154 ms
53,148 KB
testcase_54 AC 158 ms
53,176 KB
testcase_55 AC 158 ms
53,120 KB
testcase_56 AC 157 ms
53,244 KB
testcase_57 AC 156 ms
53,160 KB
testcase_58 AC 161 ms
53,172 KB
testcase_59 AC 160 ms
53,264 KB
testcase_60 AC 154 ms
53,196 KB
testcase_61 AC 157 ms
53,116 KB
testcase_62 AC 158 ms
53,192 KB
testcase_63 AC 388 ms
74,000 KB
testcase_64 AC 4 ms
7,568 KB
testcase_65 AC 3 ms
5,412 KB
testcase_66 AC 4 ms
7,692 KB
testcase_67 AC 199 ms
67,132 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

constexpr int inf = 1001001001;

int cnt[3001][3001],dp[3001][3001];
stack<int> st[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 = 2; i <= N; i++) {
        vector<int>rimit(N);
        stack<int>st;
        st.push(i-2);
        rimit[i-2] = N-1;
        for(int j = i-1; j < N; j++) {
            while (true) {
                int t = st.top();
                if(rimit[t] >= j) {
                    dp[j][i] = dp[t][i-1]+cnt[t][j];
                    break;
                }
                else {
                    st.pop();
                }
            }
            while(true && !st.empty()) {
                int t = st.top();
                if(dp[t][i-1]+cnt[t][rimit[t]] < dp[j][i-1]+cnt[j][rimit[t]]) {
                    int l = j,r = N;
                    while (l+1 < r) {
                        int mid = (l+r)/2;
                        if(dp[t][i-1]+cnt[t][mid] >= dp[j][i-1]+cnt[j][mid]) {
                            l = mid;
                        }
                        else {
                            r = mid;
                        }
                    }
                    st.push(j);
                    rimit[j] = l;
                    break;
                }
                else {
                    st.pop();
                }
            }
            if(st.empty()) {
                st.push(j);
                rimit[j] = N-1;
            }
        }
    }
    for(int i = 1; i <= N; i++) {
        cout << dp[N-1][i] << '\n';
    }
}
0