結果

問題 No.1867 Partitions and Inversions
ユーザー ぷらぷら
提出日時 2022-03-05 00:05:13
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 478 ms / 5,000 ms
コード長 2,177 bytes
コンパイル時間 3,074 ms
コンパイル使用メモリ 208,432 KB
実行使用メモリ 74,296 KB
最終ジャッジ日時 2023-09-26 03:59:05
合計ジャッジ時間 21,866 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
5,384 KB
testcase_01 AC 4 ms
7,492 KB
testcase_02 AC 422 ms
73,996 KB
testcase_03 AC 420 ms
74,008 KB
testcase_04 AC 424 ms
74,008 KB
testcase_05 AC 424 ms
74,212 KB
testcase_06 AC 427 ms
74,008 KB
testcase_07 AC 422 ms
74,064 KB
testcase_08 AC 417 ms
74,284 KB
testcase_09 AC 410 ms
74,004 KB
testcase_10 AC 423 ms
74,072 KB
testcase_11 AC 427 ms
74,228 KB
testcase_12 AC 426 ms
74,068 KB
testcase_13 AC 432 ms
74,012 KB
testcase_14 AC 432 ms
74,036 KB
testcase_15 AC 425 ms
74,296 KB
testcase_16 AC 425 ms
74,008 KB
testcase_17 AC 425 ms
74,016 KB
testcase_18 AC 430 ms
74,140 KB
testcase_19 AC 431 ms
74,220 KB
testcase_20 AC 419 ms
73,992 KB
testcase_21 AC 418 ms
74,012 KB
testcase_22 AC 427 ms
74,136 KB
testcase_23 AC 419 ms
74,080 KB
testcase_24 AC 417 ms
74,208 KB
testcase_25 AC 423 ms
74,076 KB
testcase_26 AC 433 ms
74,224 KB
testcase_27 AC 429 ms
74,072 KB
testcase_28 AC 437 ms
74,012 KB
testcase_29 AC 478 ms
74,012 KB
testcase_30 AC 429 ms
74,084 KB
testcase_31 AC 425 ms
74,008 KB
testcase_32 AC 3 ms
5,388 KB
testcase_33 AC 3 ms
7,568 KB
testcase_34 AC 4 ms
7,488 KB
testcase_35 AC 4 ms
7,796 KB
testcase_36 AC 4 ms
7,496 KB
testcase_37 AC 4 ms
7,492 KB
testcase_38 AC 12 ms
18,508 KB
testcase_39 AC 12 ms
18,408 KB
testcase_40 AC 12 ms
18,556 KB
testcase_41 AC 13 ms
18,416 KB
testcase_42 AC 12 ms
18,340 KB
testcase_43 AC 154 ms
53,240 KB
testcase_44 AC 154 ms
53,240 KB
testcase_45 AC 156 ms
53,372 KB
testcase_46 AC 157 ms
53,172 KB
testcase_47 AC 158 ms
53,180 KB
testcase_48 AC 156 ms
53,240 KB
testcase_49 AC 158 ms
53,296 KB
testcase_50 AC 156 ms
53,312 KB
testcase_51 AC 162 ms
53,176 KB
testcase_52 AC 160 ms
53,172 KB
testcase_53 AC 155 ms
53,252 KB
testcase_54 AC 156 ms
53,252 KB
testcase_55 AC 157 ms
53,228 KB
testcase_56 AC 155 ms
53,196 KB
testcase_57 AC 159 ms
53,176 KB
testcase_58 AC 160 ms
53,384 KB
testcase_59 AC 156 ms
53,240 KB
testcase_60 AC 156 ms
53,176 KB
testcase_61 AC 156 ms
53,392 KB
testcase_62 AC 159 ms
53,228 KB
testcase_63 AC 397 ms
74,012 KB
testcase_64 AC 3 ms
7,448 KB
testcase_65 AC 3 ms
5,488 KB
testcase_66 AC 4 ms
7,596 KB
testcase_67 AC 242 ms
67,180 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];
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(!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