結果

問題 No.2029 Swap Min Max Min
ユーザー t98slider
提出日時 2022-08-06 12:00:35
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 63 ms / 2,000 ms
コード長 727 bytes
コンパイル時間 1,539 ms
コンパイル使用メモリ 169,904 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-09-16 08:18:30
合計ジャッジ時間 5,477 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 42
権限があれば一括ダウンロードができます

ソースコード

diff #

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

int main(){
    int n;
    cin >> n;
    vector<int> a(n), pos;
    for(int i = 0; i < n; i++){
        cin >> a[i];
        if(a[i] <= n / 2)pos.push_back(i);
    }
    ll ans = 0;
    if(n & 1){
        for(int i = 0; i < pos.size(); i++){
            ans += abs(2 * i + 1 - pos[i]);
        }
    }else{
        ll s = 0;
        for(int i = 0; i < pos.size(); i++){
            s += abs(2 * i + 1 - pos[i]);
        }
        ans = s;
        for(int i = pos.size() - 1; i >= 0; i--){
            s -= abs(2 * i + 1 - pos[i]);
            s += abs(2 * i - pos[i]);
            ans = min(ans, s);
        }
    }
    cout << n / 2 << " " << ans << endl;
}
0