結果

問題 No.366 ロボットソート
コンテスト
ユーザー Zhan1794
提出日時 2026-02-10 17:33:28
言語 C++17
(gcc 15.2.0 + boost 1.89.0)
コンパイル:
g++-15 -O2 -lm -std=c++17 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
WA  
実行時間 -
コード長 1,327 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 1,965 ms
コンパイル使用メモリ 214,744 KB
実行使用メモリ 16,196 KB
最終ジャッジ日時 2026-02-10 17:33:35
合計ジャッジ時間 6,003 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 4 WA * 7 TLE * 1 -- * 11
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <bits/stdc++.h>

using namespace std;

#define int long long

#define all(v) (v).begin(), (v).end()
#define bit(mask, i) (((mask) >> (i)) & 1)
#define debug(x) #x << " = " << x

typedef long long ll;
typedef long double ld;

mt19937 rng(7405);

int Rand(int l, int r){
    // rng() trả về một số bất kì
    if (l > r) return r;
    return abs((int)rng() % (r - l + 1)) + l;
}

const int N = 300 + 5;

int n, a[N], k;

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    // freopen("input.inp", "r", stdin);
    // freopen("output.out", "w", stdout);
    cin >> n;
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
    }
    cin >> k;
    int ans = 0;
    for (int it = 1; it <= 300; it++) {
        if (is_sorted(a + 1, a + n + 1)) {
            break;
        }
        // int r = Rand(1, (int)(n / k - 1));
        for (int i = 1; i <= n; i++) {
            int imin = i, mn = 1e9;
            for (int j = i; j <= n; j += k) {
                if (a[j] < mn) {
                    mn = a[j];
                    imin = j;
                }
            }
            if (a[i] > a[imin]) {
                swap(a[i], a[imin]);
                ans++;
            }
        }
    }
    if (!is_sorted(a + 1, a + n + 1)) {
        ans = -1;
    }
    cout << ans << '\n';
    return 0;
}
0