結果

問題 No.366 ロボットソート
ユーザー odan3240odan3240
提出日時 2016-04-29 23:05:26
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 3 ms / 2,000 ms
コード長 1,250 bytes
コンパイル時間 606 ms
コンパイル使用メモリ 76,344 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-28 15:11:38
合計ジャッジ時間 1,649 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 1 ms
4,376 KB
testcase_17 AC 2 ms
4,376 KB
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 2 ms
4,376 KB
testcase_20 AC 2 ms
4,380 KB
testcase_21 AC 2 ms
4,380 KB
testcase_22 AC 3 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

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(j-K<0) continue;
                //printf("debug: %d %d\n",j-K,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