結果

問題 No.366 ロボットソート
ユーザー odan3240
提出日時 2016-04-29 22:56:04
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
RE  
実行時間 -
コード長 1,164 bytes
コンパイル時間 757 ms
コンパイル使用メモリ 76,556 KB
実行使用メモリ 6,820 KB
最終ジャッジ日時 2024-10-04 18:36:00
合計ジャッジ時間 1,933 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 17 WA * 4 RE * 2
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <map>
#include <set>
#include <queue>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>

using namespace std;
using ll = long long;

#define all(c) (c).begin(), (c).end()
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define pb(e) push_back(e)
#define mp(a, b) make_pair(a, b)
#define fr first
#define sc second

const ll INF = 1e9;
const ll MOD = 1e9 + 7;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

int N, K;
vector<int> a;
int main() {
    cin >> N >> K;
    a.resize(N);
    rep(i, N) cin >> a[i];
    if (is_sorted(all(a))) {
        cout << "0" << endl;
        return 0;
    }
    int ans = 0;
    rep(k, K) {
        for (int i = k; i < N - K + k; i += K) {
            for (int j = N - K + k; j > i; j -= K) {
                //printf("debug: %d %d %d\n", k, i, j);
                if (a[j - K] > a[j]) {
                    swap(a[j - K], a[j]);
                    ans++;
                }
            }
        }
    }
    if(!is_sorted(all(a))) cout<<"-1"<<endl;
    else cout<<ans<<endl;
    return 0;
}
0