結果

問題 No.2254 Reverse Only
ユーザー milanis48663220milanis48663220
提出日時 2023-03-24 22:05:28
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 75 ms / 2,000 ms
コード長 3,833 bytes
コンパイル時間 1,786 ms
コンパイル使用メモリ 139,044 KB
実行使用メモリ 14,148 KB
最終ジャッジ日時 2023-10-18 21:05:22
合計ジャッジ時間 5,033 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 1 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 75 ms
6,416 KB
testcase_09 AC 58 ms
14,148 KB
testcase_10 AC 41 ms
4,832 KB
testcase_11 AC 75 ms
6,416 KB
testcase_12 AC 74 ms
6,416 KB
testcase_13 AC 41 ms
4,832 KB
testcase_14 AC 41 ms
4,832 KB
testcase_15 AC 41 ms
4,832 KB
testcase_16 AC 50 ms
14,148 KB
testcase_17 AC 50 ms
14,144 KB
testcase_18 AC 75 ms
6,416 KB
testcase_19 AC 61 ms
6,416 KB
testcase_20 AC 58 ms
14,148 KB
testcase_21 AC 58 ms
14,148 KB
testcase_22 AC 44 ms
14,144 KB
testcase_23 AC 58 ms
14,144 KB
testcase_24 AC 49 ms
14,148 KB
testcase_25 AC 50 ms
14,148 KB
testcase_26 AC 50 ms
14,148 KB
testcase_27 AC 3 ms
4,348 KB
testcase_28 AC 17 ms
4,348 KB
testcase_29 AC 20 ms
4,348 KB
testcase_30 AC 30 ms
4,568 KB
testcase_31 AC 11 ms
4,348 KB
testcase_32 AC 12 ms
4,348 KB
testcase_33 AC 7 ms
4,348 KB
testcase_34 AC 26 ms
4,348 KB
testcase_35 AC 17 ms
4,348 KB
testcase_36 AC 63 ms
5,888 KB
testcase_37 AC 66 ms
6,152 KB
testcase_38 AC 9 ms
4,348 KB
testcase_39 AC 59 ms
5,624 KB
testcase_40 AC 3 ms
4,348 KB
testcase_41 AC 16 ms
4,348 KB
testcase_42 AC 9 ms
4,348 KB
testcase_43 AC 26 ms
4,348 KB
testcase_44 AC 38 ms
4,832 KB
testcase_45 AC 43 ms
5,096 KB
testcase_46 AC 3 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <tuple>
#include <cmath>
#include <numeric>
#include <functional>
#include <cassert>

#define debug_value(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << #x << "=" << x << endl;
#define debug(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << x << endl;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

using namespace std;
typedef long long ll;

template<typename T>
vector<vector<T>> vec2d(int n, int m, T v){
    return vector<vector<T>>(n, vector<T>(m, v));
}

template<typename T>
vector<vector<vector<T>>> vec3d(int n, int m, int k, T v){
    return vector<vector<vector<T>>>(n, vector<vector<T>>(m, vector<T>(k, v)));
}

template<typename T>
void print_vector(vector<T> v, char delimiter=' '){
    if(v.empty()) {
        cout << endl;
        return;
    }
    for(int i = 0; i+1 < v.size(); i++) cout << v[i] << delimiter;
    cout << v.back() << endl;
}

vector<int> f(vector<int> p, int l, int r){
    reverse(p.begin()+l, p.begin()+r);
    return p;
}

void test(int n, int k){
    vector<int> p(n);
    iota(p.begin(), p.end(), 0);
    set<vector<int>> st;
    queue<vector<int>> que;
    auto push = [&](vector<int> v){
        if(st.count(v) == 0){
            que.push(v);
            st.insert(v);
        }
    };
    push(p);
    while(!que.empty()){
        auto v = que.front(); que.pop();
        for(int l = 0; l < n; l++){
            for(int r = l+k; r <= n; r++){
                push(f(v, l, r));
            }
        }
    }
    for(auto v: st) print_vector(v);
    cout << st.size() << endl;
}

bool same_as_multiset(vector<int> a, vector<int> b){
    sort(a.begin(), a.end());
    sort(b.begin(), b.end());
    return a == b;
}

vector<int> z_algorithm(vector<int> s){
    int n = s.size();
    vector<int> ans(n);
    ans[0] = n;
    int i = 1, j = 0;
    while(i < s.size()){
        while(i+j < s.size() && s[i+j] == s[j]) j++;
        if(j == 0) {
            i++;
            continue;
        }
        ans[i] = j;
        int k = 1;
        while(k < j && ans[k]+k < j){
            ans[i+k] = ans[k];
            k++;
        }
        i += k;
        j -= k;
    }
    return ans;
}

bool is_cyclic_shift(vector<int> a, vector<int> b){
    int n = a.size();
    auto v = b;
    for(int x: a) v.push_back(x);
    for(int x: a) v.push_back(x);
    auto z = z_algorithm(v);
    for(int i = n; i < 2*n; i++){
        if(z[i] >= n) return true;
    }
    return false;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << setprecision(10) << fixed;
    int n, k; cin >> n >> k;
    // test(n, k);
    vector<int> a(n), b(n);
    for(int i = 0; i < n; i++) cin >> a[i];
    for(int i = 0; i < n; i++) cin >> b[i];
    if(k <= n-2){
        if(same_as_multiset(a, b)){
            cout << "Yes" << endl;
        }else{
            cout << "No" << endl;
        }
        return 0;
    }
    if(k > n){
        if(a == b){
            cout << "Yes" << endl;
        }else{
            cout << "No" << endl;
        }
        return 0;
    }
    if(k == n){
        if(a == b){
            cout << "Yes" << endl;
        }else{
            reverse(b.begin(), b.end());
            if(a == b){
                cout << "Yes" << endl;
            }else{
                cout << "No" << endl;
            }
        }
        return 0;
    }
    if(is_cyclic_shift(a, b)){
        cout << "Yes" << endl;
        return 0;
    }
    reverse(a.begin(), a.end());
    if(is_cyclic_shift(a, b)){
        cout << "Yes" << endl;
    }else{
        cout << "No" << endl;
    }
}
0