Submission #60672297


Source Code Expand

// 時間操縦者になりたい

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

// Pragmas
// #pragma GCC optimize("Ofast")
// #pragma GCC optimize("unroll-loops")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

// Namespaces
using namespace std;
using namespace __gnu_pbds;

// Data types
using si  = short int;
using ll  = long long;
using ull = unsigned long long;
using lll = __int128;
using ld  = long double;

// Pairs
using pii  = pair<int, int>;
using psi  = pair<si, si>;
using pll  = pair<ll, ll>;
using plll = pair<lll, lll>;
using pld  = pair<ld, ld>;
#define fi first
#define se second

// PBDS
template<typename Z>
using ordered_set = tree<Z, null_type, less<Z>, rb_tree_tag, tree_order_statistics_node_update>;

// Various outputs and debug
template<typename Z, typename = void> struct is_iterable : false_type {};
template<typename Z> struct is_iterable<Z, void_t<decltype(begin(declval<Z>())),decltype(end(declval<Z>()))>> : true_type {};
template<typename Z> typename enable_if<is_iterable<Z>::value&&!is_same<Z, string>::value,ostream&>::type operator<<(ostream &os, const Z &v);

template<typename Y, typename Z> ostream& operator<<(ostream &os, const pair<Y, Z> &p) {
    return os << "(" << p.fi << ", " << p.se << ")";
}
template<class TupType, size_t... I> void printTuple(ostream& os, const TupType& _tup, index_sequence<I...>) {
    os << "(";
    (..., (os << (I == 0? "" : ", ") << get<I>(_tup)));
    os << ")";
}
template<class... Z> ostream& operator<<(ostream& os, const tuple<Z...>& _tup) {
    printTuple(os, _tup, make_index_sequence<sizeof...(Z)>());
    return os;
}
template<typename Z> typename enable_if<is_iterable<Z>::value&&!is_same<Z, string>::value,ostream&>::type operator<<(ostream &os, const Z &v) {
    os << "["; 
    for (auto it = v.begin(); it != v.end();) { os << *it; if (++it != v.end()) os << ", "; }
    return os << "]";
}

#define debug(...)    logger(cout, #__VA_ARGS__, __VA_ARGS__)
#define debugV(v, x)  vlogger(cout, #v, x, v[x]);
#define rrebug(...)   logger(cerr, #__VA_ARGS__, __VA_ARGS__)
#define rrebugV(v, x) vlogger(cerr, #v, x, v[x]);
template <typename... Args>
void logger(ostream& os, string vars, Args &&...values) {
    os << vars << " = "; string delim = "";
    (..., (os << delim << values, delim = ", "));
    os << "\n";
}
template<class Y, class Z>
void vlogger(ostream& os, string var, Y idx, Z val) {
    os << var << "[" << idx << "] = " << val << "\n";
}

// Various macros
#define All(x)            x.begin(), x.end()
#define Sort(x)           sort(All(x))
#define Reverse(x)        reverse(All(x))
#define Uniqueify(x)      Sort(x); x.erase(unique(All(x)), x.end())
#define RandomSeed        chrono::steady_clock::now().time_since_epoch().count()
#define MultipleTestcases int _tc; cin >> _tc; for (int _cur_tc = 1; _cur_tc <= _tc; _cur_tc++)

// Chmin & chmax
template<typename Z> bool chmin(Z &a, Z b) { return (b < a) ? a = b, true : false; }
template<typename Z> bool chmax(Z &a, Z b) { return (b > a) ? a = b, true : false; }
 
// Modular arithmetic
template<int MOD>
class ModInt {
  public:
    int v;
    ModInt() : v(0) {}
    ModInt(long long _v) {
        v = int((-MOD < _v && _v < MOD) ? (_v) : (_v % MOD));
        if (v < 0) v += MOD;
    }
 
    friend bool operator==(const ModInt &a, const ModInt &b) { return a.v == b.v; }
    friend bool operator!=(const ModInt &a, const ModInt &b) { return a.v != b.v; }
    friend bool operator< (const ModInt &a, const ModInt &b) { return a.v <  b.v; }
    friend bool operator<=(const ModInt &a, const ModInt &b) { return a.v <= b.v; }
    friend bool operator> (const ModInt &a, const ModInt &b) { return a.v >  b.v; }
    friend bool operator>=(const ModInt &a, const ModInt &b) { return a.v >= b.v; }
 
    ModInt &operator+=(const ModInt &a) { v = (long long)(v + a.v) % MOD; return *this; }
    ModInt &operator-=(const ModInt &a) { v = (long long)(v - a.v + MOD) % MOD; return *this; }
    ModInt &operator*=(const ModInt &a) { v = 1ll * v * a.v % MOD; return *this; }
    ModInt &operator/=(const ModInt &a) { return (*this) *= inverse(a); }
 
    friend ModInt pow(ModInt a, long long x) {
        ModInt res = 1;
        for (; x; x /= 2, a *= a) if (x & 1) res *= a;
        return res;
    }
    friend ModInt inverse(ModInt a) { return pow(a, MOD - 2); }
 
    ModInt operator+ () const { return ModInt( v); }
    ModInt operator- () const { return ModInt(-v); }
    ModInt operator++() const { return *this += 1; }
    ModInt operator--() const { return *this -= 1; }
 
    friend ModInt operator+(ModInt a, const ModInt &b) { return a += b; }
    friend ModInt operator-(ModInt a, const ModInt &b) { return a -= b; }
    friend ModInt operator*(ModInt a, const ModInt &b) { return a *= b; }
    friend ModInt operator/(ModInt a, const ModInt &b) { return a /= b; }
 
    friend istream &operator>>(istream &is, ModInt &v) { return is >> v.v; }
    friend ostream &operator<<(ostream &os, const ModInt &v) { return os << v.v; }
};
const int ModA = 998244353;
const int ModC = 1e9 + 7;
using MintA    = ModInt<ModA>;
using MintC    = ModInt<ModC>;

// Other constants
const ll INF  = 1e18;
const ll iINF = 1e9;
const ld EPS  = 1e-9;
const ld iEPS = 1e-6;

const int maxN  = 200'023;

int N, X;
int A[maxN], B[maxN];
int P[maxN], Q[maxN];
int inv_P[maxN], inv_Q[maxN];

size_t longestCommonSubsequence(const vector<int> &u, const vector<int> &v) {
    static int u_pos[maxN];
    for (size_t i = 0; i < u.size(); i++) u_pos[u[i]] = i + 1;

    vector<int> w;
    for (auto i : v) {
        if (u_pos[i]) w.push_back(u_pos[i]);
    }

    vector<int> lis;
    for (auto i : w) {
        auto it = lower_bound(All(lis), i);
        if (it == lis.end()) lis.push_back(i);
        else *it = i;
    }

    return lis.size();
}

int main() {
    ios_base::sync_with_stdio(false); cin.tie(NULL);
    
    cin >> N >> X;
    for (int i = 1; i <= N; i++) cin >> A[i];
    for (int i = 1; i <= N; i++) cin >> B[i];

    for (int i = 1; i <= N; i++) { cin >> P[i]; inv_P[P[i]] = i; }
    for (int i = 1; i <= N; i++) { cin >> Q[i]; inv_Q[Q[i]] = i; }

    const auto getCycle = [&](vector<int> &cycle, int *perm, int *val) -> int {
        while (true) {
            int nxt = perm[cycle.back()];
            if (nxt == cycle.front()) break;
            cycle.push_back(nxt);
        }
        int sum = 0;
        for (auto i : cycle) sum += val[i];
        return sum;
    };

    vector<int> cyc_P, cyc_Q;
    cyc_P = cyc_Q = {X};
    int sum_P = getCycle(cyc_P, inv_P, A);
    int sum_Q = getCycle(cyc_Q, inv_Q, B);
    if (
        (sum_P != accumulate(A+1, A+N+1, 0))
        || (sum_Q != accumulate(B+1, B+N+1, 0))
    ) {
        cout << "-1\n";
        exit(0);
    }

    cyc_P = vector(cyc_P.begin() + 1, cyc_P.end());
    cyc_Q = vector(cyc_Q.begin() + 1, cyc_Q.end());

    while (!cyc_P.empty() && A[cyc_P.back()] == 0) cyc_P.pop_back();
    while (!cyc_Q.empty() && B[cyc_Q.back()] == 0) cyc_Q.pop_back();

    // debug(cyc_P);
    // debug(cyc_Q);
    // debug(longestCommonSubsequence(cyc_P, cyc_Q));

    size_t ans = cyc_P.size() + cyc_Q.size() - longestCommonSubsequence(cyc_P, cyc_Q);
    cout << ans << "\n";
}

// dibisakan

Submission Info

Submission Time
Task C - Balls and Boxes
User Zanite
Language C++ 20 (gcc 12.2)
Score 600
Code Size 7499 Byte
Status AC
Exec Time 52 ms
Memory 12564 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 600 / 600
Status
AC × 4
AC × 60
Set Name Test Cases
Sample example0.txt, example1.txt, example2.txt, example3.txt
All 000.txt, 001.txt, 002.txt, 003.txt, 004.txt, 005.txt, 006.txt, 007.txt, 008.txt, 009.txt, 010.txt, 011.txt, 012.txt, 013.txt, 014.txt, 015.txt, 016.txt, 017.txt, 018.txt, 019.txt, 020.txt, 021.txt, 022.txt, 023.txt, 024.txt, 025.txt, 026.txt, 027.txt, 028.txt, 029.txt, 030.txt, 031.txt, 032.txt, 033.txt, 034.txt, 035.txt, 036.txt, 037.txt, 038.txt, 039.txt, 040.txt, 041.txt, 042.txt, 043.txt, 044.txt, 045.txt, 046.txt, 047.txt, 048.txt, 049.txt, 050.txt, 051.txt, 052.txt, 053.txt, 054.txt, 055.txt, example0.txt, example1.txt, example2.txt, example3.txt
Case Name Status Exec Time Memory
000.txt AC 7 ms 3372 KiB
001.txt AC 39 ms 10088 KiB
002.txt AC 40 ms 10084 KiB
003.txt AC 51 ms 11768 KiB
004.txt AC 46 ms 12564 KiB
005.txt AC 44 ms 11740 KiB
006.txt AC 34 ms 8420 KiB
007.txt AC 42 ms 10032 KiB
008.txt AC 38 ms 9676 KiB
009.txt AC 39 ms 9592 KiB
010.txt AC 37 ms 9124 KiB
011.txt AC 42 ms 10144 KiB
012.txt AC 41 ms 9820 KiB
013.txt AC 46 ms 10540 KiB
014.txt AC 42 ms 9796 KiB
015.txt AC 38 ms 9040 KiB
016.txt AC 37 ms 9104 KiB
017.txt AC 49 ms 11724 KiB
018.txt AC 40 ms 9636 KiB
019.txt AC 38 ms 9716 KiB
020.txt AC 37 ms 9564 KiB
021.txt AC 43 ms 10492 KiB
022.txt AC 41 ms 9976 KiB
023.txt AC 46 ms 10728 KiB
024.txt AC 46 ms 10604 KiB
025.txt AC 52 ms 11852 KiB
026.txt AC 51 ms 11768 KiB
027.txt AC 51 ms 11752 KiB
028.txt AC 52 ms 11748 KiB
029.txt AC 51 ms 11780 KiB
030.txt AC 52 ms 11776 KiB
031.txt AC 52 ms 11684 KiB
032.txt AC 52 ms 11748 KiB
033.txt AC 51 ms 11768 KiB
034.txt AC 51 ms 11800 KiB
035.txt AC 52 ms 11780 KiB
036.txt AC 51 ms 11768 KiB
037.txt AC 51 ms 11756 KiB
038.txt AC 51 ms 11948 KiB
039.txt AC 51 ms 11740 KiB
040.txt AC 38 ms 9672 KiB
041.txt AC 37 ms 8916 KiB
042.txt AC 37 ms 8904 KiB
043.txt AC 38 ms 9540 KiB
044.txt AC 39 ms 10056 KiB
045.txt AC 40 ms 9312 KiB
046.txt AC 37 ms 9160 KiB
047.txt AC 40 ms 10116 KiB
048.txt AC 37 ms 9216 KiB
049.txt AC 40 ms 9796 KiB
050.txt AC 8 ms 4664 KiB
051.txt AC 30 ms 7880 KiB
052.txt AC 24 ms 7276 KiB
053.txt AC 39 ms 9220 KiB
054.txt AC 36 ms 8476 KiB
055.txt AC 39 ms 9220 KiB
example0.txt AC 1 ms 3488 KiB
example1.txt AC 1 ms 3404 KiB
example2.txt AC 1 ms 3424 KiB
example3.txt AC 1 ms 3488 KiB