#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; using lint = long long; using pint = pair; using plint = pair; 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##_begin_;i--) #define REP(i, n) FOR(i,0,n) #define IREP(i, n) IFOR(i,0,n) template bool chmax(T &m, const T q) { return m < q ? (m = q, true) : false; } template bool chmin(T &m, const T q) { return m > q ? (m = q, true) : false; } const std::vector> 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 T1 floor_div(T1 num, T2 den) { return (num > 0 ? num / den : -((-num + den - 1) / den)); } template std::pair operator+(const std::pair &l, const std::pair &r) { return std::make_pair(l.first + r.first, l.second + r.second); } template std::pair operator-(const std::pair &l, const std::pair &r) { return std::make_pair(l.first - r.first, l.second - r.second); } template std::vector sort_unique(std::vector vec) { sort(vec.begin(), vec.end()), vec.erase(unique(vec.begin(), vec.end()), vec.end()); return vec; } template int arglb(const std::vector &v, const T &x) { return std::distance(v.begin(), std::lower_bound(v.begin(), v.end(), x)); } template int argub(const std::vector &v, const T &x) { return std::distance(v.begin(), std::upper_bound(v.begin(), v.end(), x)); } template IStream &operator>>(IStream &is, std::vector &vec) { for (auto &v : vec) is >> v; return is; } template OStream &operator<<(OStream &os, const std::vector &vec); template OStream &operator<<(OStream &os, const std::array &arr); template OStream &operator<<(OStream &os, const std::unordered_set &vec); template OStream &operator<<(OStream &os, const pair &pa); template OStream &operator<<(OStream &os, const std::deque &vec); template OStream &operator<<(OStream &os, const std::set &vec); template OStream &operator<<(OStream &os, const std::multiset &vec); template OStream &operator<<(OStream &os, const std::unordered_multiset &vec); template OStream &operator<<(OStream &os, const std::pair &pa); template OStream &operator<<(OStream &os, const std::map &mp); template OStream &operator<<(OStream &os, const std::unordered_map &mp); template OStream &operator<<(OStream &os, const std::tuple &tpl); template OStream &operator<<(OStream &os, const std::vector &vec) { os << '['; for (auto v : vec) os << v << ','; os << ']'; return os; } template OStream &operator<<(OStream &os, const std::array &arr) { os << '['; for (auto v : arr) os << v << ','; os << ']'; return os; } template std::istream &operator>>(std::istream &is, std::tuple &tpl) { std::apply([&is](auto &&... args) { ((is >> args), ...);}, tpl); return is; } template OStream &operator<<(OStream &os, const std::tuple &tpl) { os << '('; std::apply([&os](auto &&... args) { ((os << args << ','), ...);}, tpl); return os << ')'; } template OStream &operator<<(OStream &os, const std::unordered_set &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; } template OStream &operator<<(OStream &os, const std::deque &vec) { os << "deq["; for (auto v : vec) os << v << ','; os << ']'; return os; } template OStream &operator<<(OStream &os, const std::set &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; } template OStream &operator<<(OStream &os, const std::multiset &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; } template OStream &operator<<(OStream &os, const std::unordered_multiset &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; } template OStream &operator<<(OStream &os, const std::pair &pa) { return os << '(' << pa.first << ',' << pa.second << ')'; } template OStream &operator<<(OStream &os, const std::map &mp) { os << '{'; for (auto v : mp) os << v.first << "=>" << v.second << ','; os << '}'; return os; } template OStream &operator<<(OStream &os, const std::unordered_map &mp) { os << '{'; for (auto v : mp) os << v.first << "=>" << v.second << ','; os << '}'; return os; } #ifdef HITONANODE_LOCAL const string COLOR_RESET = "\033[0m", BRIGHT_CYAN = "\033[1;36m", 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 using State = pair>; // https://hitonanode.github.io/cplib-cpp/utilities/timer.hpp #include class timer_ { std::chrono::system_clock::time_point start_; public: timer_() : start_(now()) {} static std::chrono::system_clock::time_point now() { return std::chrono::system_clock::now(); } int spent_ms() const { auto diff = now() - start_; return std::chrono::duration_cast(diff).count(); } } timer; // https://hitonanode.github.io/cplib-cpp/random/rand_nondeterministic.hpp #include #include struct rand_int_ { using lint = long long; std::mt19937 mt; rand_int_() : mt(42) {} // rand_int_() : mt(std::chrono::steady_clock::now().time_since_epoch().count()) {} lint operator()(lint x) { return this->operator()(0, x); } // [0, x) lint operator()(lint l, lint r) { std::uniform_int_distribution d(l, r - 1); return d(mt); } } rnd; // https://hitonanode.github.io/cplib-cpp/heuristic/exponential_dist_sampler.hpp #include #include #include template struct ExponentialDistSampler { std::array minuslogps; constexpr ExponentialDistSampler() { for (int i = 0; i < (1 << D); ++i) minuslogps.at(i) = -log((0.5 + i) / (1 << D)); } double sample(uint32_t random_mask) const { return minuslogps.at(random_mask & ((1 << D) - 1)); } // p ~ U(0, 1) => -log(p) ~ Ex(1) // P[exp(-|dx| / T) >= p] = P[|dx| <= -log(p) * T] bool check_sa(double abs_dx, double T, uint32_t random_mask) const { return abs_dx <= minuslogps.at(random_mask & ((1 << D) - 1)) * T; } }; const ExponentialDistSampler<16> log_ps; static constexpr int N = 20; // int f(int i, int j) { return i * N + j; } bool in(int i, int j) { return 0 <= i and i < N and 0 <= j and j < N; } const vector dxs{1, -1, 0, 0}, dys{0, 0, 1, -1}; pair> Solve(const vector> &A, int T, int ITER) { int score = 0; vector seq; int best_score = 0; vector best_seq; vector isin(N, vector(N)); auto CheckBest = [&]() { if (chmax(best_score, score)) best_seq = seq; }; auto Insert = [&](int idx, int i, int j) { assert(!isin[i][j]); score += A[i][j]; seq.insert(seq.begin() + idx, {i, j}); isin[i][j] = true; }; auto Remove = [&](int idx) { const int i = seq[idx].first, j = seq[idx].second; assert(isin[i][j]); score -= A[i][j]; seq.erase(seq.begin() + idx); isin[i][j] = false; }; auto Change = [&](int idx, int i, int j) { const int i0 = seq[idx].first, j0 = seq[idx].second; assert(isin[i0][j0]); assert(!isin[i][j]); score += A[i][j] - A[i0][j0]; seq[idx] = {i, j}; isin[i0][j0] = false; isin[i][j] = true; }; auto IsAdj = [&](int i, int j) -> bool { auto [xi, yi] = seq.at(i); auto [xj, yj] = seq.at(j); return abs(xi - xj) + abs(yi - yj) == 1; }; auto PosWeight = [&](int i) -> int { return A[seq[i].first][seq[i].second]; }; { int a = -1; int i0 = -1, j0 = -1; REP(i, N) REP(j, N) if (chmax(a, A[i][j])) i0 = i, j0 = j; Insert(0, i0, j0); } const int BASE = 0; double init_temp = 1e4, end_temp = 1.0; REP(iter, ITER) { const double temp = init_temp * pow(end_temp / init_temp, (double)iter / ITER); const int tp = rnd(7); if (tp < 1) { // add const int pos = rnd(seq.size() + 1); if (pos == 0 or pos == (int)seq.size()) { if ((int)seq.size() + 1 <= T) { const int d = rnd(4); if (pos == 0) { const int ni = seq[0].first + dxs[d], nj = seq[0].second + dys[d]; if (in(ni, nj) and !isin[ni][nj]) { const int w = A[ni][nj] - BASE; if (w >= 0 or log_ps.check_sa(-w, temp, rnd(1 << 16))) { Insert(0, ni, nj); } } } else { const int ni = seq.back().first + dxs[d], nj = seq.back().second + dys[d]; if (in(ni, nj) and !isin[ni][nj]) { const int w = A[ni][nj] - BASE; if (w >= 0 or log_ps.check_sa(-w, temp, rnd(1 << 16))) { Insert(seq.size(), ni, nj); } } } } } else { if ((int)seq.size() + 2 <= T) { const int dx = seq[pos].first - seq[pos - 1].first; const int dy = seq[pos].second - seq[pos - 1].second; int d = -1; REP(dd, 4) if (dxs[dd] == dx and dys[dd] == dy) d = dd; assert(d != -1); const int dc = d ^ 2 ^ rnd(2); const int ni = seq[pos - 1].first + dxs[dc], nj = seq[pos - 1].second + dys[dc]; const int mi = seq[pos].first + dxs[dc], mj = seq[pos].second + dys[dc]; if (in(ni, nj) and in(mi, mj) and !isin[ni][nj] and !isin[mi][mj]) { const int w = A[ni][nj] + A[mi][mj] - BASE * 2; if (w >= 0 or log_ps.check_sa(-w, temp, rnd(1 << 16))) { Insert(pos, ni, nj); Insert(pos + 1, mi, mj); } } } } } else if (tp < 4) { // change one const int pos = rnd(seq.size()); const int dir1 = rnd(2), dir2 = rnd(2) + 2; const int i = seq[pos].first + dxs[dir1] + dxs[dir2], j = seq[pos].second + dys[dir1] + dys[dir2]; bool fail = false; if (pos) { const int di = seq[pos - 1].first - i, dj = seq[pos - 1].second - j; if (abs(di) + abs(dj) != 1) fail = true; } if (pos + 1 < (int)seq.size()) { const int di = seq[pos + 1].first - i, dj = seq[pos + 1].second - j; if (abs(di) + abs(dj) != 1) fail = true; } if (in(i, j) and !isin[i][j] and !fail) { const int w = A[i][j] - PosWeight(pos); if (w >= 0 or log_ps.check_sa(-w, temp, rnd(1 << 16))) { Change(pos, i, j); } } } else if (tp < 5) { // remove if (seq.size() > 3) { const int pos = rnd(seq.size() - 3); if (IsAdj(pos, pos + 3)) { const int w = -(PosWeight(pos + 1) + PosWeight(pos + 2)) + BASE * 2; if (w >= 0 or log_ps.check_sa(-w, temp, rnd(1 << 16))) { Remove(pos + 1); Remove(pos + 1); } } } } else if (tp < 6) { // swap if (seq.size() > 3) { int pos1 = rnd(seq.size() - 1), pos2 = rnd(seq.size() - 1); if (pos1 > pos2) swap(pos1, pos2); if (pos1 != pos2 and IsAdj(pos1, pos2) and abs(pos1 - pos2) > 1 and IsAdj(pos1 + 1, pos2 + 1)) { std::reverse(seq.begin() + pos1 + 1, seq.begin() + pos2 + 1); } } } else if (tp < 7) { // remove and add const int direction = rnd(2); const int d = rnd(4); if (direction) { const int ni = seq.back().first + dxs[d], nj = seq.back().second + dys[d]; if (in(ni, nj) and !isin[ni][nj]) { const int w = A[ni][nj] - PosWeight(0); if (w >= 0 or log_ps.check_sa(-w, temp, rnd(1 << 16))) { Remove(0); Insert(seq.size(), ni, nj); } } } else { const int ni = seq[0].first + dxs[d], nj = seq[0].second + dys[d]; if (in(ni, nj) and !isin[ni][nj]) { const int w = A[ni][nj] - PosWeight(seq.size() - 1); if (w >= 0 or log_ps.check_sa(-w, temp, rnd(1 << 16))) { Remove(seq.size() - 1); Insert(0, ni, nj); } } } } CheckBest(); } return {best_score, best_seq}; } int main() { int N_, T; cin >> N_ >> T; assert(N == N_); vector A(N, vector(N)); cin >> A; int best_score = 0; vector best_seq; REP(_, 2) { auto [score, seq] = Solve(A, T, 15000000); if (chmax(best_score, score)) { best_seq = seq; } } cout << best_seq.size() << '\n'; REP(i, best_seq.size() - 1) { auto [dx, dy] = best_seq[i + 1] - best_seq[i]; assert(abs(dx) + abs(dy) == 1); } for (auto [i, j] : best_seq) cout << i << ' ' << j << '\n'; cerr << "score = " << best_score << endl; }