結果
問題 | No.5019 Hakai Project |
ユーザー | iiljj |
提出日時 | 2023-11-18 02:32:07 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 721 ms / 3,000 ms |
コード長 | 20,355 bytes |
コンパイル時間 | 2,345 ms |
コンパイル使用メモリ | 163,728 KB |
実行使用メモリ | 6,676 KB |
スコア | 2,605,364,383 |
最終ジャッジ日時 | 2023-11-18 02:32:45 |
合計ジャッジ時間 | 35,169 ms |
ジャッジサーバーID (参考情報) |
judge15 / judge13 |
純コード判定しない問題か言語 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 502 ms
6,676 KB |
testcase_01 | AC | 652 ms
6,676 KB |
testcase_02 | AC | 721 ms
6,676 KB |
testcase_03 | AC | 459 ms
6,676 KB |
testcase_04 | AC | 421 ms
6,676 KB |
testcase_05 | AC | 584 ms
6,676 KB |
testcase_06 | AC | 525 ms
6,676 KB |
testcase_07 | AC | 619 ms
6,676 KB |
testcase_08 | AC | 509 ms
6,676 KB |
testcase_09 | AC | 708 ms
6,676 KB |
testcase_10 | AC | 399 ms
6,676 KB |
testcase_11 | AC | 602 ms
6,676 KB |
testcase_12 | AC | 598 ms
6,676 KB |
testcase_13 | AC | 553 ms
6,676 KB |
testcase_14 | AC | 459 ms
6,676 KB |
testcase_15 | AC | 501 ms
6,676 KB |
testcase_16 | AC | 497 ms
6,676 KB |
testcase_17 | AC | 341 ms
6,676 KB |
testcase_18 | AC | 599 ms
6,676 KB |
testcase_19 | AC | 473 ms
6,676 KB |
testcase_20 | AC | 594 ms
6,676 KB |
testcase_21 | AC | 519 ms
6,676 KB |
testcase_22 | AC | 419 ms
6,676 KB |
testcase_23 | AC | 625 ms
6,676 KB |
testcase_24 | AC | 636 ms
6,676 KB |
testcase_25 | AC | 527 ms
6,676 KB |
testcase_26 | AC | 623 ms
6,676 KB |
testcase_27 | AC | 379 ms
6,676 KB |
testcase_28 | AC | 498 ms
6,676 KB |
testcase_29 | AC | 366 ms
6,676 KB |
testcase_30 | AC | 506 ms
6,676 KB |
testcase_31 | AC | 572 ms
6,676 KB |
testcase_32 | AC | 637 ms
6,676 KB |
testcase_33 | AC | 425 ms
6,676 KB |
testcase_34 | AC | 611 ms
6,676 KB |
testcase_35 | AC | 563 ms
6,676 KB |
testcase_36 | AC | 515 ms
6,676 KB |
testcase_37 | AC | 540 ms
6,676 KB |
testcase_38 | AC | 539 ms
6,676 KB |
testcase_39 | AC | 478 ms
6,676 KB |
testcase_40 | AC | 721 ms
6,676 KB |
testcase_41 | AC | 615 ms
6,676 KB |
testcase_42 | AC | 495 ms
6,676 KB |
testcase_43 | AC | 412 ms
6,676 KB |
testcase_44 | AC | 666 ms
6,676 KB |
testcase_45 | AC | 436 ms
6,676 KB |
testcase_46 | AC | 453 ms
6,676 KB |
testcase_47 | AC | 542 ms
6,676 KB |
testcase_48 | AC | 616 ms
6,676 KB |
testcase_49 | AC | 656 ms
6,676 KB |
ソースコード
/* #region Head */ // #include <bits/stdc++.h> #include <algorithm> #include <array> #include <bitset> #include <cassert> // assert.h #include <cmath> // math.h #include <cstring> #include <ctime> #include <deque> #include <fstream> #include <functional> #include <iomanip> #include <iostream> #include <list> #include <map> #include <memory> #include <numeric> #include <queue> #include <random> #include <set> #include <sstream> #include <stack> #include <string> #include <unordered_map> #include <unordered_set> #include <vector> using namespace std; using ll = long long; using ull = unsigned long long; using ld = long double; using pll = pair<ll, ll>; template <class T> using vc = vector<T>; template <class T> using vvc = vc<vc<T>>; using vll = vc<ll>; using vvll = vvc<ll>; using vld = vc<ld>; using vvld = vvc<ld>; using vs = vc<string>; using vvs = vvc<string>; template <class T, class U> using um = unordered_map<T, U>; template <class T> using pq = priority_queue<T>; template <class T> using pqa = priority_queue<T, vc<T>, greater<T>>; template <class T> using us = unordered_set<T>; #define TREP(T, i, m, n) for (T i = (m), i##_len = (T)(n); i < i##_len; ++(i)) #define TREPM(T, i, m, n) for (T i = (m), i##_max = (T)(n); i <= i##_max; ++(i)) #define TREPR(T, i, m, n) for (T i = (m), i##_min = (T)(n); i >= i##_min; --(i)) #define TREPD(T, i, m, n, d) for (T i = (m), i##_len = (T)(n); i < i##_len; i += (d)) #define TREPMD(T, i, m, n, d) for (T i = (m), i##_max = (T)(n); i <= i##_max; i += (d)) #define REP(i, m, n) for (ll i = (m), i##_len = (ll)(n); i < i##_len; ++(i)) #define REPM(i, m, n) for (ll i = (m), i##_max = (ll)(n); i <= i##_max; ++(i)) #define REPR(i, m, n) for (ll i = (m), i##_min = (ll)(n); i >= i##_min; --(i)) #define REPD(i, m, n, d) for (ll i = (m), i##_len = (ll)(n); i < i##_len; i += (d)) #define REPMD(i, m, n, d) for (ll i = (m), i##_max = (ll)(n); i <= i##_max; i += (d)) #define REPI(itr, ds) for (auto itr = ds.begin(); itr != ds.end(); itr++) #define REPIR(itr, ds) for (auto itr = ds.rbegin(); itr != ds.rend(); itr++) #define ALL(x) begin(x), end(x) #define SIZE(x) ((ll)(x).size()) #define ISIZE(x) ((int)(x).size()) #define PERM(c) \ sort(ALL(c)); \ for (bool c##p = 1; c##p; c##p = next_permutation(ALL(c))) #define UNIQ(v) v.erase(unique(ALL(v)), v.end()); #define CEIL(a, b) (((a) + (b)-1) / (b)) #define endl '\n' constexpr ll INF = 1'010'000'000'000'000'017LL; constexpr int IINF = 1'000'000'007LL; constexpr ll MOD = 1'000'000'007LL; // 1e9 + 7 // constexpr ll MOD = 998244353; constexpr ld EPS = 1e-12; constexpr ld PI = 3.14159265358979323846; template <typename T> istream &operator>>(istream &is, vc<T> &vec) { // vector 入力 for (T &x : vec) is >> x; return is; } template <typename T> ostream &operator<<(ostream &os, const vc<T> &vec) { // vector 出力 (for dump) os << "{"; REP(i, 0, SIZE(vec)) os << vec[i] << (i == i_len - 1 ? "" : ", "); os << "}"; return os; } template <typename T> ostream &operator>>(ostream &os, const vc<T> &vec) { // vector 出力 (inline) REP(i, 0, SIZE(vec)) os << vec[i] << (i == i_len - 1 ? "\n" : " "); return os; } template <typename T, size_t _Nm> istream &operator>>(istream &is, array<T, _Nm> &arr) { // array 入力 REP(i, 0, SIZE(arr)) is >> arr[i]; return is; } template <typename T, size_t _Nm> ostream &operator<<(ostream &os, const array<T, _Nm> &arr) { // array 出力 (for dump) os << "{"; REP(i, 0, SIZE(arr)) os << arr[i] << (i == i_len - 1 ? "" : ", "); os << "}"; return os; } template <typename T, typename U> istream &operator>>(istream &is, pair<T, U> &pair_var) { // pair 入力 is >> pair_var.first >> pair_var.second; return is; } template <typename T, typename U> ostream &operator<<(ostream &os, const pair<T, U> &pair_var) { // pair 出力 os << "(" << pair_var.first << ", " << pair_var.second << ")"; return os; } // map, um, set, us 出力 template <class T> ostream &out_iter(ostream &os, const T &map_var) { os << "{"; REPI(itr, map_var) { os << *itr; auto itrcp = itr; if (++itrcp != map_var.end()) os << ", "; } return os << "}"; } template <typename T, typename U> ostream &operator<<(ostream &os, const map<T, U> &map_var) { return out_iter(os, map_var); } template <typename T, typename U> ostream &operator<<(ostream &os, const um<T, U> &map_var) { os << "{"; REPI(itr, map_var) { auto [key, value] = *itr; os << "(" << key << ", " << value << ")"; auto itrcp = itr; if (++itrcp != map_var.end()) os << ", "; } os << "}"; return os; } template <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) { return out_iter(os, set_var); } template <typename T> ostream &operator<<(ostream &os, const us<T> &set_var) { return out_iter(os, set_var); } template <typename T> ostream &operator<<(ostream &os, const pq<T> &pq_var) { pq<T> pq_cp(pq_var); os << "{"; if (!pq_cp.empty()) { os << pq_cp.top(), pq_cp.pop(); while (!pq_cp.empty()) os << ", " << pq_cp.top(), pq_cp.pop(); } return os << "}"; } // tuple 出力 template <size_t N = 0, bool end_line = false, typename... Args> ostream &operator<<(ostream &os, tuple<Args...> &a) { if constexpr (N < std::tuple_size_v<tuple<Args...>>) { os << get<N>(a); if constexpr (N + 1 < std::tuple_size_v<tuple<Args...>>) { os << ' '; } else if constexpr (end_line) { os << '\n'; } return operator<< <N + 1, end_line>(os, a); } return os; } template <typename... Args> void print_tuple(tuple<Args...> &a) { operator<< <0, true>(std::cout, a); } void pprint() { std::cout << endl; } template <class Head, class... Tail> void pprint(Head &&head, Tail &&...tail) { std::cout << head; if (sizeof...(Tail) > 0) std::cout << ' '; pprint(move(tail)...); } // dump #define DUMPOUT cerr void dump_func() { DUMPOUT << endl; } template <class Head, class... Tail> void dump_func(Head &&head, Tail &&...tail) { DUMPOUT << head; if (sizeof...(Tail) > 0) DUMPOUT << ", "; dump_func(move(tail)...); } // chmax (更新「される」かもしれない値が前) template <typename T, typename U, typename Comp = less<>> bool chmax(T &xmax, const U &x, Comp comp = {}) { if (comp(xmax, x)) { xmax = x; return true; } return false; } // chmin (更新「される」かもしれない値が前) template <typename T, typename U, typename Comp = less<>> bool chmin(T &xmin, const U &x, Comp comp = {}) { if (comp(x, xmin)) { xmin = x; return true; } return false; } // ローカル用 #ifndef ONLINE_JUDGE #define DEBUG_ #endif #ifndef MYLOCAL #undef DEBUG_ #endif #ifdef DEBUG_ #define DEB #define dump(...) \ DUMPOUT << " " << string(#__VA_ARGS__) << ": " \ << "[" << to_string(__LINE__) << ":" << __FUNCTION__ << "]" << endl \ << " ", \ dump_func(__VA_ARGS__) #else #define DEB if (false) #define dump(...) #endif #define VAR(type, ...) \ type __VA_ARGS__; \ assert((std::cin >> __VA_ARGS__)); template <typename T> istream &operator,(istream &is, T &rhs) { return is >> rhs; } template <typename T> ostream &operator,(ostream &os, const T &rhs) { return os << ' ' << rhs; } struct AtCoderInitialize { static constexpr int IOS_PREC = 15; static constexpr bool AUTOFLUSH = false; AtCoderInitialize() { ios_base::sync_with_stdio(false), std::cin.tie(nullptr), std::cout.tie(nullptr); std::cout << fixed << setprecision(IOS_PREC); if (AUTOFLUSH) std::cout << unitbuf; } } ATCODER_INITIALIZE; void Yn(bool p) { std::cout << (p ? "Yes" : "No") << endl; } void YN(bool p) { std::cout << (p ? "YES" : "NO") << endl; } template <typename T> constexpr void operator--(vc<T> &v, int) noexcept { for (int i = 0; i < ISIZE(v); ++i) v[i]--; } template <typename T> constexpr void operator++(vc<T> &v, int) noexcept { for (int i = 0; i < ISIZE(v); ++i) v[i]++; } /* #endregion */ // #include <atcoder/all> // using namespace atcoder; template <typename T = ll> class Rand { using dist_type = std::uniform_int_distribution<T>; T _lb; T _ub; // std::random_device rd; // Will be used to obtain a seed for the random number engine int rd; std::mt19937_64 gen; // Standard mersenne_twister_engine seeded with rd() dist_type dis; public: Rand(T lb, T ub) : _lb(lb), _ub(ub), rd(123456), gen(rd), dis(lb, ub) {} inline T operator()() noexcept { return dis(gen); } inline void set_param(T lb, T ub) noexcept { if (lb == _lb && ub == _ub) return; _lb = lb, _ub = ub; typename dist_type::param_type param(lb, ub); dis.param(param); } }; template <size_t N = 64ull> string to_bin(ll x, bool show_base = false) { std::stringstream ss; if (show_base) ss << "0b"; ss << std::bitset<N>(x); std::string s = ss.str(); return s; } constexpr int N = 50; constexpr int M = 20; // Problem void solve() { VAR(ll, n, m); assert(n == N); assert(m == M); vc<string> A(n); cin >> A; vll c(m), l(m); vvll a(m), b(m); REP(i, 0, m) { cin >> c[i], l[i]; // dump(i, c[i], l[i]); a[i].resize(l[i]); b[i].resize(l[i]); REP(j, 0, l[i]) cin >> a[i][j], b[i][j]; } // ボード準備 array<ull, N> board; fill(ALL(board), 0ull); REP(i, 0, n) REP(j, 0, n) { // dump(i, j); if (A[i][j] != '.') { board[i] |= (1ull << j); } // dump(to_bin<50>(board[i])); } // const ull mask = (1ull << N) - 1ull; // 爆弾準備 constexpr ll offset = 20; array<array<ull, 41>, M> bombs; REP(i, 0, m) { fill(ALL(bombs[i]), 0ull); // offset を足した位置にする REP(j, 0, l[i]) { const ll row = offset + a[i][j]; const ll col = offset + b[i][j]; bombs[i][row] |= (1ull << col); } } // 50x50 の盤面,20 種類の爆弾 // なるべく地元の爆弾屋から仕入れたい. // なるべく少ない爆弾でやりくりしたい. // 3000ms あるので解の改善もできるか. // まずは,どこで何を爆破させるのが得か,調べる. // sum(#,@)/c[i] が大きい位置・爆弾種類を調べる. vc<array<ll, 3>> rcbs; while (true) { // 効果的な爆弾を探す ld max_score = 0; ll argmax_row_center = -1; ll argmax_col_center = -1; ll argmax_bomb = -1; REP(row_center, 0, N) REP(col_center, 0, N) { REP(i, 0, m) { ld score = 0; // 行 [row_center-20] が bombs[i][0] と対応する // 列 [col_center-20] が bombs[i][0]&1 と対応する REP(row, max(0LL, row_center - 20), min(n, row_center + 21)) { const ll row_idx = row - (row_center - 20); if (col_center > 20) { const ull bit = board[row] & (bombs[i][row_idx] << (col_center - 20)); score += __builtin_popcountll(bit); } else { const ull bit = board[row] & (bombs[i][row_idx] >> (20 - col_center)); score += __builtin_popcountll(bit); } } score /= c[i]; if (chmax(max_score, score)) { argmax_row_center = row_center; argmax_col_center = col_center; argmax_bomb = i; } } } // dump(max_score, argmax_row_center, argmax_col_center, argmax_bomb, max_score * c[argmax_bomb]); assert(argmax_bomb != -1); rcbs.push_back({argmax_row_center, argmax_col_center, argmax_bomb}); // 爆弾を適用する { REP(row, max(0LL, argmax_row_center - 20), min(n, argmax_row_center + 21)) { const ll row_idx = row - (argmax_row_center - 20); if (argmax_col_center > 20) { const ull bit = board[row] & (bombs[argmax_bomb][row_idx] << (argmax_col_center - 20)); board[row] ^= bit; } else { const ull bit = board[row] & (bombs[argmax_bomb][row_idx] >> (20 - argmax_col_center)); board[row] ^= bit; } } } // 残り破壊対象を数える { ll obj_count = 0; REP(row, 0, n) { obj_count += __builtin_popcountll(board[row]); // } // dump(obj_count); if (obj_count == 0) break; } // break; } rcbs.insert(rcbs.begin(), array<ll, 3>{0, 0, -1}); // dump(rcbs); // dump(SIZE(rcbs)); auto cdist = [](array<ll, 3> &p0, array<ll, 3> &p1) -> ll { return abs(p1[0] - p0[0]) + abs(p1[1] - p0[1]); // }; // // debug // { // ll sum = 0; // REP(i, 0, SIZE(rcbs) - 2) { sum += cdist(rcbs[i], rcbs[i + 1]); } // dump(sum); // } // TSP として解く // 2-opt Rand<int> rand(0, SIZE(rcbs) - 2); REP(cnt, 0, 10000) { int r0 = rand(); int r1 = rand(); while (r0 == r1) r1 = rand(); if (r0 > r1) swap(r0, r1); assert(r0 < r1); if (r0 + 1 == r1) continue; // assert: 隣接していない辺同士を swap したい auto from0 = rcbs[r0]; auto to0 = rcbs[r0 + 1]; auto from1 = rcbs[r1]; auto to1 = rcbs[r1 + 1]; int dist_old = cdist(from0, to0) + cdist(from1, to1); int dist_new = cdist(from0, from1) + cdist(to0, to1); if (dist_new < dist_old) { reverse(rcbs.begin() + r0 + 1, rcbs.begin() + r1 + 1); // // debug // { // ll sum = 0; // REP(i, 0, SIZE(rcbs) - 2) { sum += cdist(rcbs[i], rcbs[i + 1]); } // dump(cnt, sum); // } } } // dump(rcbs); // dump(SIZE(rcbs)); // できるだけ直前まで残っている店で爆弾を仕入れるようにする array<ull, N> board_shop; fill(ALL(board_shop), 0ull); fill(ALL(board), 0ull); REP(i, 0, n) REP(j, 0, n) { // dump(i, j); if (A[i][j] != '.') { board[i] |= (1ull << j); } if (A[i][j] == '@') { board_shop[i] |= (1ull << j); } // dump(to_bin<50>(board[i])); } vc<array<ll, 3>> operations; REP(i, 1, SIZE(rcbs)) { // if (i == 2) { // REP(row, 0, n) { // dump(row, to_bin<50>(board_shop[row])); // // } // } // rcbs[i-1] 〜 rcbs[i] の間で店に寄るとして,どこに寄るのが最善か? // const auto [r0, c0, b0] = rcbs[i - 1]; // const auto [r1, c1, b1] = rcbs[i]; ll min_dist = IINF; ll argmin_row = -1; ll argmin_col = -1; REP(row, 0, n) REP(col, 0, n) { if ((board_shop[row] >> col) & 1) { array<ll, 3> t{row, col, -1}; // const ll dist = cdist(rcbs[i - 1], t) + cdist(rcbs[i], t); const ll dist = cdist(rcbs[i], t); if (chmin(min_dist, dist)) { argmin_row = row; argmin_col = col; } } } if (argmin_row != -1) { assert(argmin_col != -1); operations.push_back({argmin_row, argmin_col, -10000 - min_dist}); } operations.push_back(rcbs[i]); // 爆弾適用 const auto [argmax_row_center, argmax_col_center, argmax_bomb] = rcbs[i]; REP(row, max(0LL, argmax_row_center - 20), min(n, argmax_row_center + 21)) { const ll row_idx = row - (argmax_row_center - 20); if (argmax_col_center > 20) { const ull bit = board[row] & (bombs[argmax_bomb][row_idx] << (argmax_col_center - 20)); board[row] ^= bit; const ull bit_shop = board_shop[row] & (bombs[argmax_bomb][row_idx] << (argmax_col_center - 20)); board_shop[row] ^= bit_shop; if (i == 2) { dump(row, bit_shop); } } else { const ull bit = board[row] & (bombs[argmax_bomb][row_idx] >> (20 - argmax_col_center)); board[row] ^= bit; const ull bit_shop = board_shop[row] & (bombs[argmax_bomb][row_idx] >> (20 - argmax_col_center)); board_shop[row] ^= bit_shop; // if (i == 2) { // dump(row, bit_shop, row_idx, to_bin<50>(bombs[argmax_bomb][row_idx]), (20 - argmax_col_center)); // if (row == 10) { // dump(row_idx, argmax_bomb); // } // } } } // if (i == 2) { // REP(row, 0, n) { // dump(row, to_bin<50>(board_shop[row])); // // } // } } // dump(operations); // dump(SIZE(operations)); // 店の訪問時に,どの操作で使う爆弾を仕入れるか調べる vvll op2idx(SIZE(operations)); vll stock; REPR(i, SIZE(operations) - 1, 0) { if (operations[i][2] >= 0) { // 爆弾を使用する操作 stock.push_back(i); } else { // 爆弾を購入する操作 op2idx[i] = stock; stock.clear(); } } // dump(op2idx); // return; ll row = 0; ll col = 0; vc<string> ans; REP(i, 0, SIZE(operations)) { if (operations[i][2] >= 0) { // 爆弾を使用する操作 // 移動する while (row < operations[i][0]) { row++; ans.push_back("1 D"); } while (row > operations[i][0]) { row--; ans.push_back("1 U"); } while (col < operations[i][1]) { col++; ans.push_back("1 R"); } while (col > operations[i][1]) { col--; ans.push_back("1 L"); } // ans.push_back("# " + to_string(row) + ", " + to_string(col)); // 爆弾を使用する ans.push_back("3 " + to_string(operations[i][2] + 1)); } else { // 爆弾を購入する操作 if (SIZE(op2idx[i]) == 0) continue; // 移動する while (row < operations[i][0]) { row++; ans.push_back("1 D"); } while (row > operations[i][0]) { row--; ans.push_back("1 U"); } while (col < operations[i][1]) { col++; ans.push_back("1 R"); } while (col > operations[i][1]) { col--; ans.push_back("1 L"); } // ans.push_back("# " + to_string(row) + ", " + to_string(col)); // 爆弾を購入する for (const ll idx : op2idx[i]) { ans.push_back("2 " + to_string(operations[idx][2] + 1)); } } } pprint(SIZE(ans)); for (const string &line : ans) { pprint(line); // } } // entry point int main() { solve(); return 0; }