結果
| 問題 | No.5025 Forest Fire |
| コンテスト | |
| ユーザー |
👑 hitonanode
|
| 提出日時 | 2026-10-24 21:44:02 |
| 言語 | C++23 (gcc 15.3.0 + boost 1.92.0 + ACL) |
| 結果 |
AC
不安定
|
| 実行時間 | 1,957 ms / 2,000 ms |
| + 988µs | |
| コード長 | 17,530 bytes |
| 記録 | |
| コンパイル時間 | 3,124 ms |
| コンパイル使用メモリ | 338,492 KB |
| 実行使用メモリ | 9,924 KB |
| スコア | 148,928,149 |
| 最終ジャッジ日時 | 2026-10-24 21:45:45 |
| 合計ジャッジ時間 | 96,505 ms |
|
ジャッジサーバーID (参考情報) |
judge5_0 / <nil> |
| 純コード判定しない問題か言語 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 50 |
ソースコード
#pragma GCC optimize("O3,unroll-loops")
#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_CYAN = "\033[1;36m";
#define dbgif(cond, x) ((cond) ? std::cerr << BRIGHT_CYAN << #x << COLOR_RESET << " = " << (x) << " (L" << __LINE__ << ") " << __FILE__ << std::endl : std::cerr)
#define dbg(x) dbgif(true, x)
#else
#define dbg(x) ((void)0)
#define dbgif(cond, x) ((void)0)
#endif
// https://hitonanode.github.io/cplib-cpp/unionfind/unionfind.hpp
#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;
}
};
// https://hitonanode.github.io/cplib-cpp/random/rand_nondeterministic.hpp
#include <cassert>
#include <chrono>
#include <cstdint>
#include <random>
struct RNGMt19937 {
using lint = long long;
std::mt19937 mt;
RNGMt19937(long long seed) : mt(seed) {}
lint operator()(lint x) { // [0, x)
assert(x > 0);
return this->operator()(0, x);
}
lint operator()(lint l, lint r) {
assert(l < r);
std::uniform_int_distribution<lint> d(l, r - 1);
return d(mt);
}
};
// RNGMt19937 rnd(123456789);
// RNGMt19937 rnd(std::chrono::steady_clock::now().time_since_epoch().count());
// Fast random number generator based on xoshiro256++.
struct FastRNG {
using ull = unsigned long long;
ull s[4];
static ull rotl(ull x, int k) { return (x << k) | (x >> (64 - k)); }
static ull splitmix64(ull &x) {
ull z = (x += 0x9e3779b97f4a7c15ULL);
z = (z ^ (z >> 30)) * 0xbf58476d1ce4e5b9ULL;
z = (z ^ (z >> 27)) * 0x94d049bb133111ebULL;
return z ^ (z >> 31);
}
FastRNG(ull seed) {
for (auto &x : s) x = splitmix64(seed);
}
ull next_u64() {
ull res = rotl(s[0] + s[3], 23) + s[0];
ull t = s[1] << 17;
s[2] ^= s[0];
s[3] ^= s[1];
s[1] ^= s[2];
s[0] ^= s[3];
s[2] ^= t;
s[3] = rotl(s[3], 45);
return res;
}
uint32_t next_u32() { return next_u64() >> 32; }
// [0, n)
uint32_t operator()(uint32_t n) {
assert(n > 0);
return (uint64_t(next_u32()) * n) >> 32;
}
// [l, r)
int operator()(int l, int r) {
assert(l < r);
uint32_t width = uint64_t(int64_t(r) - int64_t(l));
return int(int64_t(l) + (*this)(width));
}
uint16_t next_u16() { return next_u64() >> 48; }
};
inline FastRNG rnd(123456789);
// FastRNG rnd(std::chrono::steady_clock::now().time_since_epoch().count());
constexpr int N = 50, K = 100;
using InputState = array<char, N * N>;
using RandState = array<char, N * N>;
using BanState = array<bool, N * N>;
RandState Gen() {
array<char, N * N> ret{};
for (auto &c : ret) c = '0' + rnd(10);
return ret;
}
int f(int x, int y) { return x * N + y; }
bool isin(int x, int y) { return 0 <= x and x < N and 0 <= y and y < N; }
int QuadSum(const InputState &input, const RandState &rand_state, const BanState &ban_state) {
int ret = 0;
vector<int> visited(N * N);
auto IsAlive = [&](int x, int y) {
if (!isin(x, y)) return false;
const int idx = f(x, y);
if (ban_state[idx]) return false;
return input[idx] > rand_state[idx];
};
REP(i, N) REP(j, N) {
if (visited[f(i, j)]) continue;
int sum = 0;
auto rec = [&](auto &&self, int x, int y) -> void {
if (!IsAlive(x, y)) return;
const int idx = f(x, y);
if (visited[idx]) return;
visited[idx] = 1;
sum++;
self(self, x + 1, y);
self(self, x - 1, y);
self(self, x, y + 1);
self(self, x, y - 1);
};
rec(rec, i, j);
ret += sum * sum;
}
return ret;
}
void BurnProb(const InputState &input, const RandState &rand_state,
const BanState &ban_state, vector<double> &burn_weight) {
assert((int)burn_weight.size() == N * N);
UnionFind uf(N * N);
auto IsAlive = [&](int x, int y) {
const int idx = f(x, y);
if (ban_state[idx]) return false;
return !ban_state[idx] and (input[idx] > rand_state[idx]);
};
REP(i, N) REP(j, N) {
if (!IsAlive(i, j)) continue;
if (i + 1 < N and IsAlive(i + 1, j)) uf.unite(f(i, j), f(i + 1, j));
if (j + 1 < N and IsAlive(i, j + 1)) uf.unite(f(i, j), f(i, j + 1));
}
REP(i, N) REP(j, N) {
burn_weight[f(i, j)] += uf.count(f(i, j));
}
}
double CalcE(const InputState &input, const vector<RandState> &rand_states, const BanState &ban_state) {
lint quad_sum = 0;
for (const auto &rand_state : rand_states) {
quad_sum += QuadSum(input, rand_state, ban_state);
}
return double(quad_sum) / ((lint)rand_states.size() * N * N);
}
vector<double> BurnProbMap(const InputState &input, const vector<RandState> &rand_states,
const BanState &ban_state) {
vector<double> ret(N * N);
for(const auto &rand_state : rand_states) {
BurnProb(input, rand_state, ban_state, ret);
}
for (auto &x : ret) x /= (lint)rand_states.size() * N * N;
return ret;
}
BanState GenBan0Debug() {
BanState ban_state{};
vector<pint> vs{
{0, 38}, {1, 37}, {2, 36}, {3, 35}, {4, 34}, {4, 36}, {5, 33}, {5, 37}, {6, 32},
{6, 38}, {7, 31}, {7, 39}, {8, 30}, {8, 40}, {9, 41}, {9, 49}, {10, 42}, {10, 48},
{11, 43}, {11, 47}, {12, 44}, {12, 46}, {13, 29}, {13, 45}, {14, 30}, {14, 44}, {15, 31},
{15, 43}, {16, 42}, {18, 42}, {19, 43}, {20, 20}, {20, 44}, {21, 19}, {21, 45}, {22, 20},
{22, 46}, {23, 47}, {24, 48}, {25, 17}, {25, 49}, {26, 16}, {26, 48}, {27, 19}, {27, 47},
{28, 18}, {28, 26}, {28, 46}, {29, 17}, {29, 27}, {29, 45}, {30, 16}, {30, 28}, {30, 44},
{31, 15}, {31, 29}, {31, 43}, {32, 30}, {32, 42}, {33, 31}, {33, 41}, {34, 32}, {34, 40},
{35, 33}, {35, 39}, {35, 41}, {36, 34}, {36, 38}, {36, 42}, {37, 35}, {37, 37}, {37, 43},
{38, 36}, {38, 44}, {39, 15}, {39, 35}, {39, 45}, {40, 16}, {40, 34}, {40, 46}, {41, 17},
{41, 33}, {41, 47}, {42, 18}, {42, 32}, {42, 48}, {43, 19}, {43, 31}, {43, 49}, {44, 20},
{44, 30}, {45, 21}, {45, 29}, {46, 22}, {46, 28}, {47, 23}, {47, 27}, {48, 24}, {48, 26},
{49, 25},
};
for (const auto &[x, y] : vs) {
ban_state[f(x, y)] = 1;
}
return ban_state;
}
int main() {
InputState input_state{};
vector<string> S(N);
{
int N_, K_;
cin >> N_ >> K_;
assert(N == N_);
assert(K == K_);
for (auto &s : S) cin >> s;
REP(i, N) REP(j, N) input_state[f(i, j)] = S[i][j];
}
// vector<RandState> rand_states;
// REP(_, 100) rand_states.push_back(Gen());
// dbg(rand_states.size());
// const auto e0 = CalcE(input_state, rand_states, BanState{});
// dbg(e0);
// const auto ban0 = GenBan0Debug();
// REP(_, 1000) {
// const auto e1 = CalcE(input_state, rand_states, ban0);
// // dbg(e1);
// }
// const int score = round(1e6 * (1 + log2(e0 / e1)));
// dbg(score);
// dbg(BurnProbMap(input_state, rand_states, ban0));
auto IsTarget = [&](int x, int y) {
const int idx = f(x, y);
if (input_state[idx] >= '5') return true;
if (input_state[idx] < '3') return false;
for (auto [dx, dy] : grid_dxs) {
const int nx = x + dx;
const int ny = y + dy;
if (isin(nx, ny) && input_state[f(nx, ny)] >= '5') return true;
}
return false;
};
auto FastEval = [&](const BanState &ban_state) {
UnionFind uf(N * N);
REP(x, N) REP(y, N) {
if (ban_state[f(x, y)]) continue;
if (!IsTarget(x, y)) continue;
if (x + 1 < N && !ban_state[f(x + 1, y)] && IsTarget(x + 1, y)) uf.unite(f(x, y), f(x + 1, y));
if (y + 1 < N && !ban_state[f(x, y + 1)] && IsTarget(x, y + 1)) uf.unite(f(x, y), f(x, y + 1));
}
int ret = 0;
REP(idx, N * N) {
if (!IsTarget(idx / N, idx % N)) continue;
if (uf.find(idx) == idx) ret += uf.count(idx) * uf.count(idx);
}
return ret;
};
BanState state{};
while (true) {
const int k = count(ALL(state), true);
const int rem = K - k;
if (rem <= 0) break;
double max_eval = -1e200;
int argmin_x0 = -1;
int argmin_y0 = -1;
int argmin_dir = -1;
int argmin_len = -1;
const auto orig_e = FastEval(state);
REP(x0, N) REP(y0, N) {
// if (input_state[f(x0, y0)] == '0') continue;
REP(dir, 2) {
const int dx = 1;
const int dy = dir ? 1 : -1;
int nban = 0;
BanState next_state = state;
FOR(len, 1, N + 1) {
const int nx = x0 + dx * (len - 1);
const int ny = y0 + dy * (len - 1);
if (!isin(nx, ny)) break;
if (input_state[f(nx, ny)] == '0') break;
if (state[f(nx, ny)]) break;
if (chmax(next_state[f(nx, ny)], true)) {
++nban;
if (nban > 20 or nban > rem) break;
const int nnx = nx + dx;
const int nny = ny + dy;
if (!isin(nnx, nny) or !IsTarget(nnx, nny) or state[f(nnx, nny)]) {
auto e = (double)(orig_e - FastEval(next_state)) / len;
if (chmax(max_eval, e)) {
argmin_x0 = x0;
argmin_y0 = y0;
argmin_dir = dir;
argmin_len = len;
}
break;
}
}
}
}
}
dbg(make_tuple(argmin_x0, argmin_y0, argmin_dir, argmin_len, max_eval));
if (argmin_x0 != -1) {
const int dx = 1;
const int dy = argmin_dir ? 1 : -1;
FOR(len, 1, argmin_len + 1) {
const int nx = argmin_x0 + dx * (len - 1);
const int ny = argmin_y0 + dy * (len - 1);
if (!isin(nx, ny)) break;
state[f(nx, ny)] = true;
}
} else {
break;
}
}
cout << count(ALL(state), true) << "\n";
REP(i, N) REP(j, N) {
if (state[f(i, j)]) cout << i << " " << j << "\n";
}
}
hitonanode