結果

問題 No.2029 Swap Min Max Min
ユーザー RubikunRubikun
提出日時 2022-08-05 23:11:55
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 22 ms / 2,000 ms
コード長 1,431 bytes
コンパイル時間 2,275 ms
コンパイル使用メモリ 200,904 KB
実行使用メモリ 7,944 KB
最終ジャッジ日時 2023-10-14 01:00:12
合計ジャッジ時間 4,800 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,368 KB
testcase_01 AC 2 ms
4,372 KB
testcase_02 AC 1 ms
4,368 KB
testcase_03 AC 13 ms
5,236 KB
testcase_04 AC 4 ms
4,372 KB
testcase_05 AC 8 ms
4,372 KB
testcase_06 AC 15 ms
4,368 KB
testcase_07 AC 14 ms
5,700 KB
testcase_08 AC 20 ms
7,696 KB
testcase_09 AC 6 ms
4,496 KB
testcase_10 AC 5 ms
4,368 KB
testcase_11 AC 8 ms
4,608 KB
testcase_12 AC 10 ms
4,368 KB
testcase_13 AC 21 ms
7,768 KB
testcase_14 AC 21 ms
4,372 KB
testcase_15 AC 21 ms
7,656 KB
testcase_16 AC 19 ms
4,372 KB
testcase_17 AC 20 ms
7,656 KB
testcase_18 AC 22 ms
7,924 KB
testcase_19 AC 20 ms
4,372 KB
testcase_20 AC 21 ms
4,368 KB
testcase_21 AC 20 ms
4,368 KB
testcase_22 AC 20 ms
4,372 KB
testcase_23 AC 20 ms
7,712 KB
testcase_24 AC 21 ms
7,944 KB
testcase_25 AC 19 ms
4,372 KB
testcase_26 AC 19 ms
4,368 KB
testcase_27 AC 1 ms
4,368 KB
testcase_28 AC 2 ms
4,368 KB
testcase_29 AC 2 ms
4,372 KB
testcase_30 AC 2 ms
4,372 KB
testcase_31 AC 1 ms
4,368 KB
testcase_32 AC 2 ms
4,368 KB
testcase_33 AC 1 ms
4,368 KB
testcase_34 AC 1 ms
4,368 KB
testcase_35 AC 2 ms
4,368 KB
testcase_36 AC 1 ms
4,368 KB
testcase_37 AC 2 ms
4,368 KB
testcase_38 AC 19 ms
7,708 KB
testcase_39 AC 18 ms
4,368 KB
testcase_40 AC 17 ms
4,368 KB
testcase_41 AC 20 ms
7,664 KB
testcase_42 AC 19 ms
7,740 KB
testcase_43 AC 19 ms
4,368 KB
testcase_44 AC 20 ms
7,736 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return true; } return false; }
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define mp make_pair
#define si(x) int(x.size())
const int mod=998244353,MAX=600005,INF=1<<30;

ll dp[MAX][2];

int main(){
    
    std::ifstream in("text.txt");
    std::cin.rdbuf(in.rdbuf());
    cin.tie(0);
    ios::sync_with_stdio(false);
    
    int N;cin>>N;
    vector<int> A;
    for(int i=0;i<N;i++){
        int x;cin>>x;
        if(x<=N/2) A.push_back(i);
    }
    
    ll ans=1LL<<60;
    
    if(N&1){
        vector<int> B;
        for(int i=1;i<N;i+=2) B.push_back(i);
        ll sum=0;
        for(int i=0;i<si(A);i++) sum+=abs(A[i]-B[i]);
        chmin(ans,sum);
    }else{
        for(int i=0;i<N;i++) for(int j=0;j<2;j++) dp[i][j]=1LL<<60;
        dp[0][1]=0;
        for(int i=0;i<N/2;i++){
            for(int j=0;j<2;j++){
                int a=(i-1)*2+j;
                for(int k=0;k<2;k++){
                    int b=i*2+k;
                    if(b-a==3) continue;
                    chmin(dp[i+1][k],dp[i][j]+abs(A[i]-b));
                }
            }
        }
        chmin(ans,dp[N/2][0]);
        chmin(ans,dp[N/2][1]);
    }
    
    cout<<N/2<<" "<<ans<<endl;
}
0