結果

問題 No.2029 Swap Min Max Min
ユーザー RubikunRubikun
提出日時 2022-08-05 23:11:55
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 52 ms / 2,000 ms
コード長 1,431 bytes
コンパイル時間 1,867 ms
コンパイル使用メモリ 197,320 KB
最終ジャッジ日時 2025-01-30 18:44:16
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 42
権限があれば一括ダウンロードができます

ソースコード

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