結果
| 問題 |
No.366 ロボットソート
|
| コンテスト | |
| ユーザー |
siguma323
|
| 提出日時 | 2016-04-29 23:36:57 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 2,049 bytes |
| コンパイル時間 | 1,010 ms |
| コンパイル使用メモリ | 89,576 KB |
| 実行使用メモリ | 13,636 KB |
| 最終ジャッジ日時 | 2024-10-04 19:18:33 |
| 合計ジャッジ時間 | 4,806 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 18 WA * 4 TLE * 1 |
ソースコード
#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;
}
siguma323