Submission #70485677
Source Code Expand
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <deque>
#include <forward_list>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <optional>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
using lint = long long;
using pint = pair<int, int>;
using plint = pair<lint, lint>;
struct fast_ios { fast_ios(){ cin.tie(nullptr), ios::sync_with_stdio(false), cout << fixed << setprecision(20); }; } fast_ios_;
#define ALL(x) (x).begin(), (x).end()
#define FOR(i, begin, end) for(int i=(begin),i##_end_=(end);i<i##_end_;i++)
#define IFOR(i, begin, end) for(int i=(end)-1,i##_begin_=(begin);i>=i##_begin_;i--)
#define REP(i, n) FOR(i,0,n)
#define IREP(i, n) IFOR(i,0,n)
template <typename T> bool chmax(T &m, const T q) { return m < q ? (m = q, true) : false; }
template <typename T> bool chmin(T &m, const T q) { return m > q ? (m = q, true) : false; }
const std::vector<std::pair<int, int>> grid_dxs{{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
int floor_lg(long long x) { return x <= 0 ? -1 : 63 - __builtin_clzll(x); }
template <class T1, class T2> T1 floor_div(T1 num, T2 den) { return (num > 0 ? num / den : -((-num + den - 1) / den)); }
template <class T1, class T2> std::pair<T1, T2> operator+(const std::pair<T1, T2> &l, const std::pair<T1, T2> &r) { return std::make_pair(l.first + r.first, l.second + r.second); }
template <class T1, class T2> std::pair<T1, T2> operator-(const std::pair<T1, T2> &l, const std::pair<T1, T2> &r) { return std::make_pair(l.first - r.first, l.second - r.second); }
template <class T> std::vector<T> sort_unique(std::vector<T> vec) { sort(vec.begin(), vec.end()), vec.erase(unique(vec.begin(), vec.end()), vec.end()); return vec; }
template <class T> int arglb(const std::vector<T> &v, const T &x) { return std::distance(v.begin(), std::lower_bound(v.begin(), v.end(), x)); }
template <class T> int argub(const std::vector<T> &v, const T &x) { return std::distance(v.begin(), std::upper_bound(v.begin(), v.end(), x)); }
template <class IStream, class T> IStream &operator>>(IStream &is, std::vector<T> &vec) { for (auto &v : vec) is >> v; return is; }
template <class OStream, class T> OStream &operator<<(OStream &os, const std::vector<T> &vec);
template <class OStream, class T, size_t sz> OStream &operator<<(OStream &os, const std::array<T, sz> &arr);
template <class OStream, class T, class TH> OStream &operator<<(OStream &os, const std::unordered_set<T, TH> &vec);
template <class OStream, class T, class U> OStream &operator<<(OStream &os, const pair<T, U> &pa);
template <class OStream, class T> OStream &operator<<(OStream &os, const std::deque<T> &vec);
template <class OStream, class T> OStream &operator<<(OStream &os, const std::set<T> &vec);
template <class OStream, class T> OStream &operator<<(OStream &os, const std::multiset<T> &vec);
template <class OStream, class T> OStream &operator<<(OStream &os, const std::unordered_multiset<T> &vec);
template <class OStream, class T, class U> OStream &operator<<(OStream &os, const std::pair<T, U> &pa);
template <class OStream, class TK, class TV> OStream &operator<<(OStream &os, const std::map<TK, TV> &mp);
template <class OStream, class TK, class TV, class TH> OStream &operator<<(OStream &os, const std::unordered_map<TK, TV, TH> &mp);
template <class OStream, class... T> OStream &operator<<(OStream &os, const std::tuple<T...> &tpl);
template <class OStream, class T> OStream &operator<<(OStream &os, const std::vector<T> &vec) { os << '['; for (auto v : vec) os << v << ','; os << ']'; return os; }
template <class OStream, class T, size_t sz> OStream &operator<<(OStream &os, const std::array<T, sz> &arr) { os << '['; for (auto v : arr) os << v << ','; os << ']'; return os; }
template <class... T> std::istream &operator>>(std::istream &is, std::tuple<T...> &tpl) { std::apply([&is](auto &&... args) { ((is >> args), ...);}, tpl); return is; }
template <class OStream, class... T> OStream &operator<<(OStream &os, const std::tuple<T...> &tpl) { os << '('; std::apply([&os](auto &&... args) { ((os << args << ','), ...);}, tpl); return os << ')'; }
template <class OStream, class T, class TH> OStream &operator<<(OStream &os, const std::unordered_set<T, TH> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; }
template <class OStream, class T> OStream &operator<<(OStream &os, const std::deque<T> &vec) { os << "deq["; for (auto v : vec) os << v << ','; os << ']'; return os; }
template <class OStream, class T> OStream &operator<<(OStream &os, const std::set<T> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; }
template <class OStream, class T> OStream &operator<<(OStream &os, const std::multiset<T> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; }
template <class OStream, class T> OStream &operator<<(OStream &os, const std::unordered_multiset<T> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; }
template <class OStream, class T, class U> OStream &operator<<(OStream &os, const std::pair<T, U> &pa) { return os << '(' << pa.first << ',' << pa.second << ')'; }
template <class OStream, class TK, class TV> OStream &operator<<(OStream &os, const std::map<TK, TV> &mp) { os << '{'; for (auto v : mp) os << v.first << "=>" << v.second << ','; os << '}'; return os; }
template <class OStream, class TK, class TV, class TH> OStream &operator<<(OStream &os, const std::unordered_map<TK, TV, TH> &mp) { os << '{'; for (auto v : mp) os << v.first << "=>" << v.second << ','; os << '}'; return os; }
#ifdef HITONANODE_LOCAL
const string COLOR_RESET = "\033[0m", BRIGHT_GREEN = "\033[1;32m", BRIGHT_RED = "\033[1;31m", BRIGHT_CYAN = "\033[1;36m", NORMAL_CROSSED = "\033[0;9;37m", RED_BACKGROUND = "\033[1;41m", NORMAL_FAINT = "\033[0;2m";
#define dbg(x) std::cerr << BRIGHT_CYAN << #x << COLOR_RESET << " = " << (x) << NORMAL_FAINT << " (L" << __LINE__ << ") " << __FILE__ << COLOR_RESET << std::endl
#define dbgif(cond, x) ((cond) ? std::cerr << BRIGHT_CYAN << #x << COLOR_RESET << " = " << (x) << NORMAL_FAINT << " (L" << __LINE__ << ") " << __FILE__ << COLOR_RESET << std::endl : std::cerr)
#else
#define dbg(x) ((void)0)
#define dbgif(cond, x) ((void)0)
#endif
vector<vector<int>> Expe(const vector<int> &init) {
map<vector<int>, int> dp;
const int n = init.size();
using P = pair<int, vector<int>>;
priority_queue<P, vector<P>, greater<P>> pq;
pq.emplace(0, init);
dp[init] = 0;
// const int thres = n / 2 + 1;
while (pq.size()) {
auto [dist, state] = pq.top();
pq.pop();
// if (dist >= thres) continue;
if (dp.at(state) < dist) continue;
FOR(w, 1, n / 2 + 1) {
REP(l, n - w + 1) {
const int lsum = accumulate(state.begin() + l, state.begin() + l + w, 0);
FOR(r, l + w, n - w + 1) {
const int rsum = accumulate(state.begin() + r, state.begin() + r + w, 0);
if (lsum != rsum) continue;
vector<int> nxt{state.begin(), state.begin() + l};
FOR(i, r, r + w) nxt.push_back(state.at(i));
FOR(i, l + w, r) nxt.push_back(state.at(i));
FOR(i, l, l + w) nxt.push_back(state.at(i));
FOR(i, r + w, n) nxt.push_back(state.at(i));
assert(state.size() == nxt.size());
if (!dp.contains(nxt) or dp.at(nxt) > dist + 1) {
dp[nxt] = dist + 1;
pq.emplace(dist + 1, nxt);
}
}
}
}
}
// dbg(init);
// for (auto [state, dist] : dp) dbg(make_tuple(dist, state));
// dbg(dp.size());
vector<vector<int>> ret;
for (auto [state, dist] : dp) ret.push_back(state);
return ret;
}
#include <algorithm>
#include <numeric>
#include <utility>
#include <vector>
// UnionFind Tree (0-indexed), based on size of each disjoint set
struct UnionFind {
std::vector<int> par, cou;
UnionFind(int N = 0) : par(N), cou(N, 1) { iota(par.begin(), par.end(), 0); }
int find(int x) { return (par[x] == x) ? x : (par[x] = find(par[x])); }
bool unite(int x, int y) {
x = find(x), y = find(y);
if (x == y) return false;
if (cou[x] < cou[y]) std::swap(x, y);
par[y] = x, cou[x] += cou[y];
return true;
}
int count(int x) { return cou[find(x)]; }
bool same(int x, int y) { return find(x) == find(y); }
std::vector<std::vector<int>> groups() {
std::vector<std::vector<int>> ret(par.size());
for (int i = 0; i < int(par.size()); ++i) ret[find(i)].push_back(i);
ret.erase(std::remove_if(ret.begin(), ret.end(),
[&](const std::vector<int> &v) { return v.empty(); }),
ret.end());
return ret;
}
};
#include <cassert>
#include <cstdint>
#include <vector>
// Sorted set of integers [0, n)
// Space complexity: (64 / 63) n + O(log n) bit
class fast_set {
static constexpr int B = 64;
int n;
int cnt;
std::vector<std::vector<uint64_t>> _d;
static int bsf(uint64_t x) { return __builtin_ctzll(x); }
static int bsr(uint64_t x) { return 63 - __builtin_clzll(x); }
public:
// 0 以上 n_ 未満の整数が入れられる sorted set を作成
fast_set(int n_) : n(n_), cnt(0) {
do { n_ = (n_ + B - 1) / B, _d.push_back(std::vector<uint64_t>(n_)); } while (n_ > 1);
}
bool contains(int i) const {
assert(0 <= i and i < n);
return (_d.front().at(i / B) >> (i % B)) & 1;
}
void insert(int i) {
assert(0 <= i and i < n);
if (contains(i)) return;
++cnt;
for (auto &vec : _d) {
bool f = vec.at(i / B);
vec.at(i / B) |= 1ULL << (i % B), i /= B;
if (f) break;
}
}
void erase(int i) {
assert(0 <= i and i < n);
if (!contains(i)) return;
--cnt;
for (auto &vec : _d) {
vec.at(i / B) &= ~(1ULL << (i % B)), i /= B;
if (vec.at(i)) break;
}
}
// i 以上の最小要素 なければ default_val
int next(int i, const int default_val) const {
assert(0 <= i and i <= n);
for (auto itr = _d.cbegin(); itr != _d.cend(); ++itr, i = i / B + 1) {
if (i / B >= int(itr->size())) break;
if (auto d = itr->at(i / B) >> (i % B); d) {
i += bsf(d);
while (itr != _d.cbegin()) i = i * B + bsf((--itr)->at(i));
return i;
}
}
return default_val;
}
int next(const int i) const { return next(i, n); }
// i 以下の最小要素 なければ default_val
int prev(int i, int default_val = -1) const {
assert(-1 <= i and i < n);
for (auto itr = _d.cbegin(); itr != _d.cend() and i >= 0; ++itr, i = i / B - 1) {
if (auto d = itr->at(i / B) << (B - 1 - i % B); d) {
i += bsr(d) - (B - 1);
while (itr != _d.cbegin()) i = i * B + bsr((--itr)->at(i));
return i;
}
}
return default_val;
}
// return minimum element (if exists) or `n` (empty)
int min() const { return next(0); }
// return maximum element (if exists) or `-1` (empty)
int max() const { return prev(n - 1); }
int size() const { return cnt; }
bool empty() const { return cnt == 0; }
void clear() {
if (!cnt) return;
cnt = 0;
auto rec = [&](auto &&self, int d, int x) -> void {
if (d) {
for (auto m = _d.at(d).at(x); m;) {
int i = bsf(m);
m -= 1ULL << i, self(self, d - 1, x * B + i);
}
}
_d.at(d).at(x) = 0;
};
rec(rec, _d.size() - 1, 0);
}
};
pair<bool, vector<tuple<int, int, int, int>>> Solve(const vector<int> &A, const vector<int> &B) {
const int n = A.size();
vector<int> from1, to1;
vector<int> from0, to0;
REP(i, n) {
(A.at(i) ? from1 : from0).push_back(i);
(B.at(i) ? to1 : to0).push_back(i);
}
if (from1.size() != to1.size()) return {false, {}};
{
lint s = 0;
REP(i, n) {
if (A.at(i)) s += i + 1;
if (B.at(i)) s -= i + 1;
}
// dbg(s);
if (s) return {false, {}};
}
vector<int> from, to;
if (from1.size() < from0.size()) {
from = from1;
to = to1;
} else {
from = from0;
to = to0;
}
deque<pint> to_left, to_right;
REP(i, from.size()) {
const int f = from.at(i), t = to.at(i);
if (f > t) to_left.emplace_back(f, t);
if (f < t) to_right.emplace_back(f, t);
}
vector<tuple<int, int, int, int>> sol;
while (to_left.size()) {
auto [fl, tl] = to_left.front();
if (fl == tl) {
to_left.pop_front();
continue;
}
auto [fr, tr] = to_right.back();
const int dx = min(abs(fl - tl), abs(fr - tr));
const int a = fl - dx + 1, b = fl + 1, c = fr + 1, d = fr + dx + 1;
if (a < c) {
sol.emplace_back(a, b, c, d);
} else {
sol.emplace_back(c, d, a, b);
}
fl -= dx;
fr += dx;
to_left.pop_front();
if (fl != tl) to_left.emplace_front(fl, tl);
to_right.pop_back();
if (fr != tr) to_right.emplace_back(fr, tr);
}
return {true, sol};
}
void Validate(const vector<int> &A, const vector<int> &B) {
const int n = A.size();
const auto seqs = Expe(A);
const bool bru = find(ALL(seqs), B) != seqs.end();
auto [flg, ops] = Solve(A, B);
if (bru != flg) {
dbg(make_tuple(A, B, bru, flg));
assert(false);
}
if (!flg) return;
assert((int)ops.size() <= (int)A.size() / 2);
// dbg(ops);
auto now = A;
for (auto [a, b, c, d] : ops) {
--a, --c;
assert(b - a == d - c);
assert(a < b and b <= c and c < d);
assert(accumulate(now.begin() + a, now.begin() + b, 0) ==
accumulate(now.begin() + c, now.begin() + d, 0));
vector<int> vs;
REP(i, a) vs.push_back(now.at(i));
FOR(i, c, d) vs.push_back(now.at(i));
FOR(i, b, c) vs.push_back(now.at(i));
FOR(i, a, b) vs.push_back(now.at(i));
FOR(i, d, n) vs.push_back(now.at(i));
if (now.size() != vs.size()) {
dbg(now);
dbg(vs);
dbg(make_tuple(a, b, c, d, n));
}
assert(now.size() == vs.size());
now = vs;
}
if (now != B) {
dbg(make_tuple(A, now, B));
dbg(ops);
}
assert(now == B);
}
int main() {
// Expe({0, 0, 1, 1, 0, 0, 1, 1});
// Expe({0, 1, 0, 1, 0, 1, 0, 1});
FOR(n, 1, 9) {
break;
dbg(n);
vector<vector<int>> states;
vector<vector<vector<int>>> tos;
REP(S, 1 << n) {
vector<int> init(n);
REP(i, n) init.at(i) = (S >> i) & 1;
states.push_back(init);
auto e = Expe(init);
tos.push_back(e);
}
map<vector<int>, int> mp;
REP(S, 1 << n) mp[states.at(S)] = S;
UnionFind uf(1 << n);
REP(S, 1 << n) {
for (auto e : tos.at(S)) {
uf.unite(S, mp.at(e));
}
}
for (auto g : uf.groups()) {
vector<vector<int>> vs;
for (auto e : g) vs.push_back(states.at(e));
sort(ALL(vs));
// dbg(vs);
}
REP(S, 1 << n) {
REP(T, 1 << n) {
auto A = states.at(S);
auto B = states.at(T);
Validate(A, B);
}
}
}
int T;
cin >> T;
while (T--) {
int N;
cin >> N;
vector<int> A(N), B(N);
cin >> A >> B;
// dbg(A);
// Expe(A);
// dbg(B);
auto [flg, sol] = Solve(A, B);
if (flg) {
cout << "Yes\n" << sol.size() << '\n';
for (auto [a, b, c, d] : sol) cout << a << ' ' << b << ' ' << c << ' ' << d << '\n';
} else {
cout << "No\n";
}
}
}
Submission Info
Judge Result
| Set Name |
Sample |
All |
| Score / Max Score |
0 / 0 |
800 / 800 |
| Status |
|
|
| Set Name |
Test Cases |
| Sample |
sample.txt |
| All |
random_1.txt, random_10.txt, random_11.txt, random_12.txt, random_13.txt, random_14.txt, random_15.txt, random_16.txt, random_17.txt, random_18.txt, random_19.txt, random_2.txt, random_20.txt, random_21.txt, random_22.txt, random_23.txt, random_24.txt, random_25.txt, random_26.txt, random_27.txt, random_28.txt, random_29.txt, random_3.txt, random_30.txt, random_4.txt, random_5.txt, random_6.txt, random_7.txt, random_8.txt, random_9.txt, sample.txt, small_1.txt, small_10.txt, small_11.txt, small_12.txt, small_13.txt, small_14.txt, small_15.txt, small_16.txt, small_2.txt, small_3.txt, small_4.txt, small_5.txt, small_6.txt, small_7.txt, small_8.txt, small_9.txt |
| Case Name |
Status |
Exec Time |
Memory |
| random_1.txt |
AC |
18 ms |
5192 KiB |
| random_10.txt |
AC |
25 ms |
5516 KiB |
| random_11.txt |
AC |
15 ms |
6024 KiB |
| random_12.txt |
AC |
25 ms |
7800 KiB |
| random_13.txt |
AC |
31 ms |
10028 KiB |
| random_14.txt |
AC |
22 ms |
6012 KiB |
| random_15.txt |
AC |
18 ms |
5500 KiB |
| random_16.txt |
AC |
23 ms |
6640 KiB |
| random_17.txt |
AC |
20 ms |
4740 KiB |
| random_18.txt |
AC |
28 ms |
7132 KiB |
| random_19.txt |
AC |
20 ms |
5940 KiB |
| random_2.txt |
AC |
18 ms |
6116 KiB |
| random_20.txt |
AC |
24 ms |
5704 KiB |
| random_21.txt |
AC |
20 ms |
6280 KiB |
| random_22.txt |
AC |
18 ms |
6636 KiB |
| random_23.txt |
AC |
29 ms |
8072 KiB |
| random_24.txt |
AC |
21 ms |
5372 KiB |
| random_25.txt |
AC |
22 ms |
5264 KiB |
| random_26.txt |
AC |
29 ms |
7288 KiB |
| random_27.txt |
AC |
18 ms |
6084 KiB |
| random_28.txt |
AC |
21 ms |
4704 KiB |
| random_29.txt |
AC |
27 ms |
8404 KiB |
| random_3.txt |
AC |
17 ms |
5188 KiB |
| random_30.txt |
AC |
19 ms |
5148 KiB |
| random_4.txt |
AC |
20 ms |
6292 KiB |
| random_5.txt |
AC |
16 ms |
5140 KiB |
| random_6.txt |
AC |
16 ms |
6040 KiB |
| random_7.txt |
AC |
23 ms |
8492 KiB |
| random_8.txt |
AC |
17 ms |
6272 KiB |
| random_9.txt |
AC |
26 ms |
7448 KiB |
| sample.txt |
AC |
1 ms |
3432 KiB |
| small_1.txt |
AC |
24 ms |
3456 KiB |
| small_10.txt |
AC |
25 ms |
3456 KiB |
| small_11.txt |
AC |
25 ms |
3516 KiB |
| small_12.txt |
AC |
26 ms |
3500 KiB |
| small_13.txt |
AC |
26 ms |
3424 KiB |
| small_14.txt |
AC |
25 ms |
3528 KiB |
| small_15.txt |
AC |
25 ms |
3516 KiB |
| small_16.txt |
AC |
5 ms |
3456 KiB |
| small_2.txt |
AC |
24 ms |
3408 KiB |
| small_3.txt |
AC |
24 ms |
3640 KiB |
| small_4.txt |
AC |
26 ms |
3560 KiB |
| small_5.txt |
AC |
26 ms |
3508 KiB |
| small_6.txt |
AC |
25 ms |
3360 KiB |
| small_7.txt |
AC |
25 ms |
3496 KiB |
| small_8.txt |
AC |
26 ms |
3512 KiB |
| small_9.txt |
AC |
25 ms |
3512 KiB |