結果

問題 No.5019 Hakai Project
ユーザー iiljj
提出日時 2023-11-18 02:32:07
言語 C++17(gcc12)
(gcc 12.3.0 + boost 1.87.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
純コード判定しない問題か言語
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 50
権限があれば一括ダウンロードができます

ソースコード

diff #
プレゼンテーションモードにする

/* #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;
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0