結果

問題 No.366 ロボットソート
ユーザー siguma323siguma323
提出日時 2016-04-29 23:36:57
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,049 bytes
コンパイル時間 1,081 ms
コンパイル使用メモリ 89,936 KB
実行使用メモリ 13,760 KB
最終ジャッジ日時 2024-04-15 06:05:15
合計ジャッジ時間 4,805 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <algorithm>
#include <numeric>
#include <string>
#include <vector>
#include <iostream>
#include <queue>
#include <utility>
#include <cmath>
#include <bitset>

using namespace std;

using ull = unsigned long long;

bool check(int index1,int index2,int k)
{
    int sub = abs(index1 - index2);
    if(sub % k == 0) return true;
    else return false;
}

bool Fillter(vector<pair<int,int>>& checker,pair<int,int> test)
{
    for(const auto& x : checker)
    {
        if(x.first == test.first && x.second == test.second)
            return false;
    }

    return true;
}

int main()
{
    ull n,k;
    cin >> n >> k;
    vector<ull> a(n);
    vector<pair<int,int>> checker;

    for(auto&& x : a)
    {
        cin >> x;
    }

    vector<ull> sorted_a = a;
    sort(sorted_a.begin(),sorted_a.end());

    if(k >= n)
    {
        if(sorted_a == a)
        {
            cout << 0 << endl;
            return 0;
        }
        else
        {
            cout << -1 << endl;
            return 0;
        }
    }

    ull count = 0;
    ull i = 0;

    for(int i =0; i < n; ++i)
    {
        auto iter1 = find(a.begin(), a.end(),a.at(i));
        int index1 = distance(a.begin(), iter1);
        auto iter2 = find(sorted_a.begin(), sorted_a.end(),a.at(i));
        int index2 = distance(sorted_a.begin(), iter2);

        if(!check(index1,index2,k))
        {
            cout << -1 << endl;
            return 0;
        }
    }

    while(a != sorted_a)
    {
        if(sorted_a.at(i) != a.at(i))
        {
            if(i+k < n)
            {
                if(Fillter(checker,make_pair(a.at(i),a.at(i+k))))
                {
                    checker.emplace_back(make_pair(a.at(i),a.at(i+k)));
                    swap(a.at(i),a.at(i+k));
                    ++count;
                }
                else
                {
                    cout << -1 << endl;
                    return 0;
                }
            }
        }
        ++i;

        if(i == n)
            i = 0;
    }

    cout << count << endl;



}
0