結果

問題 No.1867 Partitions and Inversions
ユーザー ぷらぷら
提出日時 2022-03-04 23:54:30
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 437 ms / 5,000 ms
コード長 2,215 bytes
コンパイル時間 2,403 ms
コンパイル使用メモリ 208,348 KB
実行使用メモリ 74,312 KB
最終ジャッジ日時 2023-09-26 03:25:17
合計ジャッジ時間 22,624 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
5,436 KB
testcase_01 AC 4 ms
7,432 KB
testcase_02 AC 427 ms
74,064 KB
testcase_03 AC 416 ms
74,004 KB
testcase_04 AC 414 ms
74,264 KB
testcase_05 AC 431 ms
74,004 KB
testcase_06 AC 426 ms
74,200 KB
testcase_07 AC 414 ms
74,072 KB
testcase_08 AC 418 ms
74,036 KB
testcase_09 AC 419 ms
74,016 KB
testcase_10 AC 417 ms
74,072 KB
testcase_11 AC 425 ms
73,988 KB
testcase_12 AC 419 ms
74,288 KB
testcase_13 AC 425 ms
74,064 KB
testcase_14 AC 424 ms
74,292 KB
testcase_15 AC 426 ms
73,984 KB
testcase_16 AC 419 ms
74,028 KB
testcase_17 AC 421 ms
74,016 KB
testcase_18 AC 417 ms
74,052 KB
testcase_19 AC 437 ms
74,004 KB
testcase_20 AC 417 ms
74,024 KB
testcase_21 AC 414 ms
74,312 KB
testcase_22 AC 423 ms
74,132 KB
testcase_23 AC 422 ms
74,096 KB
testcase_24 AC 426 ms
74,004 KB
testcase_25 AC 421 ms
74,048 KB
testcase_26 AC 416 ms
74,132 KB
testcase_27 AC 422 ms
74,080 KB
testcase_28 AC 419 ms
74,044 KB
testcase_29 AC 429 ms
74,008 KB
testcase_30 AC 428 ms
74,036 KB
testcase_31 AC 425 ms
74,260 KB
testcase_32 AC 4 ms
5,384 KB
testcase_33 AC 4 ms
7,564 KB
testcase_34 AC 4 ms
7,492 KB
testcase_35 AC 4 ms
7,472 KB
testcase_36 AC 4 ms
7,536 KB
testcase_37 AC 4 ms
7,512 KB
testcase_38 AC 12 ms
18,388 KB
testcase_39 AC 12 ms
18,376 KB
testcase_40 AC 12 ms
18,356 KB
testcase_41 AC 12 ms
18,332 KB
testcase_42 AC 13 ms
18,340 KB
testcase_43 AC 153 ms
53,216 KB
testcase_44 AC 154 ms
53,460 KB
testcase_45 AC 152 ms
53,220 KB
testcase_46 AC 155 ms
53,196 KB
testcase_47 AC 159 ms
53,192 KB
testcase_48 AC 153 ms
53,308 KB
testcase_49 AC 155 ms
53,276 KB
testcase_50 AC 156 ms
53,196 KB
testcase_51 AC 159 ms
53,216 KB
testcase_52 AC 158 ms
53,196 KB
testcase_53 AC 154 ms
53,452 KB
testcase_54 AC 156 ms
53,192 KB
testcase_55 AC 158 ms
53,228 KB
testcase_56 AC 154 ms
53,168 KB
testcase_57 AC 159 ms
53,220 KB
testcase_58 AC 157 ms
53,168 KB
testcase_59 AC 157 ms
53,232 KB
testcase_60 AC 157 ms
53,460 KB
testcase_61 AC 158 ms
53,164 KB
testcase_62 AC 157 ms
53,224 KB
testcase_63 AC 383 ms
74,164 KB
testcase_64 AC 4 ms
7,752 KB
testcase_65 AC 3 ms
5,476 KB
testcase_66 AC 4 ms
7,544 KB
testcase_67 AC 231 ms
66,968 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
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