提出 #15313282
ソースコード 拡げる
// This is free and unencumbered software released into the public domain.
// Anyone is free to copy, modify, publish, use, compile, sell, or
// distribute this software, either in source code form or as a compiled
// binary, for any purpose, commercial or non-commercial, and by any
// means.
// In jurisdictions that recognize copyright laws, the author or authors
// of this software dedicate any and all copyright interest in the
// software to the public domain. We make this dedication for the benefit
// of the public at large and to the detriment of our heirs and
// successors. We intend this dedication to be an overt act of
// relinquishment in perpetuity of all present and future rights to this
// software under copyright law.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
// OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
// For more information, please refer to <http://unlicense.org>
/****************/
/* template.hpp */
/****************/
#include <algorithm>
#include <cassert>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
using std::cerr;
using std::cout;
using std::endl;
using std::max;
using std::min;
using std::swap;
struct BoolName : std::numpunct<char> {
std::string t, f;
BoolName(std::string t, std::string f) : t(t), f(f) {}
std::string do_truename() const { return t; }
std::string do_falsename() const { return f; }
};
void setBoolName(std::string t, std::string f) {
cout.imbue(std::locale(cout.getloc(), new BoolName(t, f)));
}
struct Initializer {
Initializer() {
cout << std::fixed << std::setprecision(15) << std::boolalpha;
setBoolName("Yes", "No");
}
} initializer;
struct Input {
bool eof;
Input() : eof(false) {}
operator bool() {
int v;
this->eof = (std::scanf("%d", &v) != 1);
return v;
}
operator char() {
char v;
while (!(this->eof = (std::scanf("%c", &v) != 1)) && std::isspace(v)) {
}
return v;
}
operator int() {
int v;
this->eof = (std::scanf("%d", &v) != 1);
return v;
}
operator long() {
long v;
this->eof = (std::scanf("%ld", &v) != 1);
return v;
}
operator long long() {
long long v;
this->eof = (std::scanf("%lld", &v) != 1);
return v;
}
operator unsigned int() {
unsigned int v;
this->eof = (std::scanf("%u", &v) != 1);
return v;
}
operator unsigned long() {
unsigned long v;
this->eof = (std::scanf("%lu", &v) != 1);
return v;
}
operator unsigned long long() {
unsigned long long v;
this->eof = (std::scanf("%llu", &v) != 1);
return v;
}
operator double() {
double v;
this->eof = (std::scanf("%lf", &v) != 1);
return v;
}
operator long double() {
long double v;
this->eof = (std::scanf("%Lf", &v) != 1);
return v;
}
void ignore() const { getchar(); }
} in;
template <typename T> T abs(T a) { return a >= 0 ? a : -a; }
template <typename T, typename S> bool chmin(T &a, const S &b) {
return a > b ? a = b, true : false;
}
template <typename T, typename S> bool chmax(T &a, const S &b) {
return a < b ? a = b, true : false;
}
template <typename T, typename S> std::function<S(T)> cast() {
return [](const T &t) { return static_cast<S>(t); };
}
template <typename T> T copy(const T &a) { return T(a); }
class ZeroPadding {
public:
ZeroPadding(int n) : n(n) {}
int n;
};
std::ostream &operator<<(std::ostream &os, const ZeroPadding &z) {
os << std::setw(z.n) << std::setfill('0');
return os;
}
template <typename T> constexpr T inf() {
return std::numeric_limits<T>::max() / 2 - 1;
}
/*********************/
/* bit_operation.hpp */
/*********************/
template <typename T> int least_bit(T n) {
static_assert(sizeof(T) == 4 || sizeof(T) == 8, "unsupported size");
if (sizeof(T) == 4) {
return __builtin_ffs(n) - 1;
}
if (sizeof(T) == 8) {
return __builtin_ffsll(n) - 1;
}
}
// n must be greater than 0.
template <typename T> int least_bit_fast(T n) {
static_assert(sizeof(T) == 4 || sizeof(T) == 8, "unsupported size");
if (sizeof(T) == 4) {
return __builtin_ctz(n);
}
if (sizeof(T) == 8) {
return __builtin_ctzll(n);
}
}
template <typename T> int most_bit(T n) {
static_assert(sizeof(T) == 4 || sizeof(T) == 8, "unsupported size");
if (sizeof(T) == 4) {
return n ? 31 - __builtin_clz(n) : -1;
}
if (sizeof(T) == 8) {
return n ? 63 - __builtin_clzll(n) : -1;
}
}
template <typename T> int count_bit(T n) {
static_assert(sizeof(T) == 4 || sizeof(T) == 8, "unsupported size");
if (sizeof(T) == 4) {
return __builtin_popcount(n);
}
if (sizeof(T) == 8) {
return __builtin_popcountll(n);
}
}
template <typename T> int bit_parity(T n) {
static_assert(sizeof(T) == 4 || sizeof(T) == 8, "unsupported size");
if (sizeof(T) == 4) {
return __builtin_parity(n);
}
if (sizeof(T) == 8) {
return __builtin_parityll(n);
}
}
/*************/
/* tuple.hpp */
/*************/
#include <tuple>
template <typename... T> class Tuple : public std::tuple<T...> {
public:
Tuple(Input &in) : std::tuple<T...>() { (void)in; }
};
template <typename T, typename... S>
class Tuple<T, S...> : public std::tuple<T, S...> {
public:
Tuple() : std::tuple<T, S...>() {}
Tuple(T t, S... s) : std::tuple<T, S...>(t, s...) {}
Tuple(const std::tuple<T, S...> &t) : std::tuple<T, S...>(t) {}
Tuple(Input &in) {
auto a = std::tuple<T>(in);
std::tuple<S...> b = Tuple<S...>(in);
std::tuple<T, S...> c = std::tuple_cat(a, b);
*this = c;
}
template <int n> auto &get() { return std::get<n>(*this); }
template <int n> const auto &get() const { return std::get<n>(*this); }
};
template <typename... T> Tuple<T...> makeTuple(const T &... args) {
return Tuple<T...>(args...);
}
namespace std {
template <typename... T>
class tuple_size<Tuple<T...>>
: public std::integral_constant<size_t, sizeof...(T)> {};
template <std::size_t I, typename... T> class tuple_element<I, Tuple<T...>> {
public:
using type = tuple_element_t<I, std::tuple<T...>>;
};
} // namespace std
/*****************/
/* container.hpp */
/*****************/
#include <vector>
template <typename T> class Container : public T {
private:
using S = typename T::value_type;
using Itr = typename T::iterator;
public:
Container() : T() {}
Container(int n) : T(n) {}
Container(int n, S s) : T(n, s) {}
template <typename Itr> Container(Itr first, Itr last) : T(first, last) {}
Container(const std::initializer_list<S> &v) : T(v) {}
Container(int n, Input &in) {
std::vector<S> v(n);
for (auto &i : v) {
i = in;
}
*this = Container<T>(v.begin(), v.end());
}
S max() const { return *std::max_element(this->begin(), this->end()); }
template <typename Function> auto max(Function func) const {
std::vector<std::pair<decltype(func(S())), S>> res;
for (const auto &i : *this) {
res.emplace_back(func(i), i);
}
return std::max_element(res.begin(), res.end())->second;
}
S min() const { return *std::min_element(this->begin(), this->end()); }
Tuple<S, S> minmax() const {
auto itrs = std::minmax_element(this->begin(), this->end());
return Tuple<S, S>(*itrs.first, *itrs.second);
}
template <typename Function> auto min(Function func) const {
std::vector<std::pair<decltype(func(S())), S>> res;
for (const auto &i : *this) {
res.emplace_back(func(i), i);
}
return std::min_element(res.begin(), res.end())->second;
}
int argmax() const {
return std::distance(this->begin(),
std::max_element(this->begin(), this->end()));
}
int argmin() const {
return std::distance(this->begin(),
std::min_element(this->begin(), this->end()));
}
int find(const S &a) const {
return std::distance(this->begin(),
std::find(this->begin(), this->end(), a));
}
template <typename Function> int find_if(Function func) const {
return std::distance(this->begin(),
std::find_if(this->begin(), this->end(), func));
}
bool contains(const S &a) const {
return std::find(this->begin(), this->end(), a) != this->end();
}
int size() const { return T::size(); }
std::pair<Itr, Itr> equal_range(const S &a) {
return std::equal_range(this->begin(), this->end(), a);
}
template <typename Function> bool all_of(Function func) const {
return std::all_of(this->begin(), this->end(), func);
}
template <typename Function> bool any_of(Function func) const {
return std::any_of(this->begin(), this->end(), func);
}
template <typename Function> bool none_of(Function func) const {
return std::none_of(this->begin(), this->end(), func);
}
int count(const S &s) const {
return std::count(this->begin(), this->end(), s);
}
template <typename Function> int count_if(Function func) const {
return std::count_if(this->begin(), this->end(), func);
}
bool is_sorted() const { return std::is_sorted(this->begin(), this->end()); }
void output(std::string sep = "\n", std::string end = "\n") const {
bool first = true;
for (const auto &i : *this) {
if (!first) {
cout << sep;
}
first = false;
cout << i;
}
cout << end;
}
};
/***********/
/* map.hpp */
/***********/
#include <map>
template <typename T, typename S> class Map : public Container<std::map<T, S>> {
public:
Map() : Container<std::map<T, S>>() {}
bool contains(const T &a) const { return this->count(a) != 0; }
int count(const T &t) const { return std::map<T, S>::count(t); }
};
/***************/
/* ordered.hpp */
/***************/
template <typename T> class Ordered {
public:
template <typename V> bool operator==(const V &v) const {
return !(static_cast<T>(v) < static_cast<const T &>(*this) ||
static_cast<const T &>(*this) < static_cast<T>(v));
}
template <typename V> bool operator!=(const V &v) const {
return static_cast<T>(v) < static_cast<const T &>(*this) ||
static_cast<const T &>(*this) < static_cast<T>(v);
}
template <typename V> bool operator>(const V &v) const {
return static_cast<T>(v) < static_cast<const T &>(*this);
}
template <typename V> bool operator<=(const V &v) const {
return !(static_cast<T>(v) < static_cast<const T &>(*this));
}
template <typename V> bool operator>=(const V &v) const {
return !(static_cast<const T &>(*this) < static_cast<T>(v));
}
};
/**************/
/* vector.hpp */
/**************/
#include <numeric>
template <typename T> class Vector : public Container<std::vector<T>> {
public:
Vector() = default;
Vector(const Vector<T> &v) = default;
Vector(int n) : Container<std::vector<T>>(n) {}
Vector(int n, T t) : Container<std::vector<T>>(n, t) {}
template <typename Itr>
Vector(Itr first, Itr last) : Container<std::vector<T>>(first, last) {}
Vector(const std::initializer_list<T> &v) : Container<std::vector<T>>(v) {}
Vector(int n, Input &in) : Container<std::vector<T>>(n, in) {}
Vector &operator+=(const Vector &v) {
if (this->size() < v.size()) {
this->resize(v.size());
}
for (int i = 0; i < v.size(); ++i) {
(*this)[i] += v[i];
}
return *this;
}
Vector &operator+=(const T &v) {
for (auto &i : *this) {
i += v;
}
return *this;
}
Vector &operator-=(const Vector &v) {
if (this->size() < v.size()) {
this->resize(v.size());
}
for (int i = 0; i < v.size(); ++i) {
(*this)[i] -= v[i];
}
return *this;
}
Vector &operator-=(const T &v) {
for (auto &i : *this) {
i -= v;
}
return *this;
}
Vector &operator*=(const Vector &v) {
for (int i = 0; i < this->size(); ++i) {
(*this)[i] *= v[i];
}
return *this;
}
Vector &operator*=(const T &v) {
for (auto &i : *this) {
i *= v;
}
return *this;
}
Vector &operator/=(const Vector &v) {
for (int i = 0; i < this->size(); ++i) {
(*this)[i] /= v[i];
}
return *this;
}
Vector &operator/=(const T &v) {
for (auto &i : *this) {
i /= v;
}
return *this;
}
Vector &operator%=(const Vector &v) {
for (int i = 0; i < this->size(); ++i) {
(*this)[i] %= v[i];
}
return *this;
}
Vector &operator%=(const T &v) {
for (auto &i : *this) {
i %= v;
}
return *this;
}
Vector operator+(const Vector &v) const { return Vector(*this) += v; }
Vector operator+(const T &v) const { return Vector(*this) += v; }
Vector operator-(const Vector &v) const { return Vector(*this) -= v; }
Vector operator-(const T &v) const { return Vector(*this) -= v; }
Vector operator*(const Vector &v) const { return Vector(*this) *= v; }
Vector operator*(const T &v) const { return Vector(*this) *= v; }
Vector operator/(const Vector &v) const { return Vector(*this) /= v; }
Vector operator/(const T &v) const { return Vector(*this) /= v; }
Vector operator%(const Vector &v) const { return Vector(*this) %= v; }
Vector operator%(const T &v) const { return Vector(*this) %= v; }
Vector operator-() const { return *this * -1; }
T inner_product(const Vector<T> &v) const {
return std::inner_product(this->begin(), this->end(), v.begin(), T(0));
}
Vector<T> &partial_sort(int k, bool reverse = false) {
if (!reverse) {
std::partial_sort(this->begin(), this->begin() + k, this->end());
} else {
std::partial_sort(this->begin(), this->begin() + k, this->end(),
std::greater<T>());
}
return *this;
}
Vector<T> &sort() {
std::sort(this->begin(), this->end());
return *this;
}
template <typename Function> Vector<T> &sort(Function func) {
std::sort(this->begin(), this->end(), func);
return *this;
}
Vector<T> &rsort() {
std::sort(this->rbegin(), this->rend());
return *this;
}
Vector<int> argsort() const {
Vector<Tuple<T, int>> v;
for (int i = 0; i < this->size(); ++i) {
v.emplace_back((*this)[i], i);
}
v.sort();
auto f = [](const Tuple<T, int> &t) { return t.template get<1>(); };
return v.transform(f);
}
Vector<T> &nth_element(int n, bool reverse = false) {
if (!reverse) {
std::nth_element(this->begin(), this->begin() + n, this->end());
} else {
std::nth_element(this->begin(), this->begin() + n, this->end(),
std::greater<T>());
}
return *this;
}
Vector<T> subvector(int a, int b = -1) const {
if (b == -1) {
return Vector<T>(this->begin() + a, this->end());
}
return Vector<T>(this->begin() + a, this->begin() + b);
}
template <typename Function> auto transform(Function func) const {
Vector<decltype(func(T()))> res;
std::transform(this->begin(), this->end(), std::back_inserter(res), func);
return res;
}
Vector<T> partial_sum() const {
Vector<T> res;
std::partial_sum(this->begin(), this->end(), std::back_inserter(res));
return res;
}
template <typename Function> Vector<T> partial_sum(Function func) const {
Vector<T> res;
std::partial_sum(this->begin(), this->end(), std::back_inserter(res), func);
return res;
}
Vector<T> &reverse() {
std::reverse(this->begin(), this->end());
return *this;
}
Vector<T> adjacent_difference() const {
Vector<T> res;
std::adjacent_difference(this->begin(), this->end(),
std::back_inserter(res));
return res;
}
T lower_bound(T t) const {
return std::lower_bound(this->begin(), this->end(), t) - this->begin();
}
T upper_bound(T t) const {
return std::upper_bound(this->begin(), this->end(), t) - this->begin();
}
T accumulate() const {
return std::accumulate(this->begin(), this->end(), T());
}
template <typename S, typename Function>
S accumulate(S n, Function func) const {
return std::accumulate(this->begin(), this->end(), n, func);
}
Vector<T> maximum(T t) const {
return this->transform([&](T i) { return max(i, t); });
}
Vector<T> minimum(T t) const {
return this->transform([&](T i) { return min(i, t); });
}
template <typename Int> static Vector<T> makeVector(Int n) {
return Vector<T>(n);
}
template <typename Int> static Vector<T> makeVector(Input &in, Int n) {
return Vector<T>(n, in);
}
template <typename Int, typename... Ints>
static auto makeVector(Input &in, Int n, Ints... ints) {
Vector<decltype(makeVector(in, ints...))> res;
for (int i = 0; i < n; ++i) {
res.emplace_back(makeVector(in, ints...));
}
return res;
}
template <typename Int, typename... Ints>
static auto makeVector(Int n, Ints... ints) {
Vector<decltype(makeVector(ints...))> res;
for (int i = 0; i < n; ++i) {
res.emplace_back(makeVector(ints...));
}
return res;
}
Vector<T> &unique() {
this->erase(std::unique(this->begin(), this->end()), this->end());
return *this;
}
bool next_permutation() {
return std::next_permutation(this->begin(), this->end());
}
Vector<T> &rotate(int n) {
std::rotate(this->begin(), this->begin() + n, this->end());
return *this;
}
Map<T, int> countAll() const {
Map<T, int> res;
for (const auto &i : *this) {
++res[i];
}
return res;
}
T matmul(const T &a) const {
return this->transform([&](const T &i) { return i.inner_product(a); });
}
};
template <typename T> Vector<T> iota(int n, T m = 0) {
Vector<T> v(n);
std::iota(v.begin(), v.end(), m);
return v;
}
template <typename T, typename S> void read(Vector<T> &t, Vector<S> &s) {
for (int i = 0; i < t.size(); ++i) {
t[i] = T(in);
s[i] = S(in);
}
}
template <typename T, typename S, typename U>
void read(Vector<T> &t, Vector<S> &s, Vector<U> &u) {
for (int i = 0; i < t.size(); ++i) {
t[i] = T(in);
s[i] = S(in);
u[i] = U(in);
}
}
template <typename T> Vector<T> operator+(const T &a, const Vector<T> &b) {
return b + a;
}
template <typename T> Vector<T> operator-(const T &a, const Vector<T> &b) {
return -b + a;
}
template <typename T> Vector<T> operator*(const T &a, const Vector<T> &b) {
return b * a;
}
/************/
/* math.hpp */
/************/
#include <cmath>
template <typename T = double> constexpr T pi() { return acos(T(-1)); }
template <typename T> T gcd(T t) { return abs(t); }
template <typename T, typename... S> T gcd(T a, S... s) {
a = abs(a);
auto b = gcd(s...);
if (a == 0 || b == 0) {
return max(a, b);
}
int fa = least_bit_fast(a);
int fb = least_bit_fast(b);
a >>= fa;
b >>= fb;
while (a != b) {
auto &c = a > b ? a : b;
c = abs(a - b);
c >>= least_bit_fast(c);
}
return a << min(fa, fb);
}
template <typename T> T gcd(const Vector<T> &v) {
T g = abs(v[0]);
for (int i = 1; i < int(v.size()); ++i) {
g = gcd(g, v[i]);
}
return g;
}
template <typename T> T lcm(T t) { return abs(t); }
template <typename T, typename... S> T lcm(T t, S... s) {
T l = lcm(s...);
return abs(t) / gcd(t, l) * l;
}
template <typename T> T lcm(const Vector<T> &v) {
T l = abs(v[0]);
for (int i = 1; i < int(v.size()); ++i) {
l = lcm(l, v[i]);
}
return l;
}
template <typename T> T floor(T a, T b) {
auto d = std::div(a, b);
return d.quot - (d.rem && (a < 0) != (b < 0) ? 1 : 0);
}
template <typename T> T ceil(T a, T b) {
auto d = std::div(a, b);
return d.quot + (d.rem && (a > 0) == (b > 0) ? 1 : 0);
}
template <typename T> T round(T a) { return std::round(a); }
template <typename T> T round(T a, T b) { return floor(a + b / 2, b); }
template <typename T> T mod(T a, T b) {
T c = a % b;
return c < 0 ? c + abs(b) : c;
}
template <typename T> T factorial(T n) {
return n <= 1 ? 1 : factorial(n - 1) * n;
}
template <typename T> Vector<T> factorial_vector(int n) {
Vector<T> v(n + 1, 1);
for (int i = 1; i <= n; ++i) {
v[i] = v[i - 1] * i;
}
return v;
}
template <typename T> T square(T n) { return n * n; }
template <typename T> T cube(T n) { return n * n * n; }
template <typename T> T norm(T x1, T y1, T x2, T y2) {
return square(x1 - x2) + square(y1 - y2);
}
template <typename T> bool isSquare(T n) { return square(T(sqrt(n))) == n; }
template <typename T> T clamp(T v, T l, T u) {
return v < l ? l : v > u ? u : v;
}
template <typename T> auto hypot(T a, T b) { return std::hypot(a, b); }
template <typename T> auto pow(T a, T b) { return std::pow(a, b); }
template <typename T> auto log10(T a) { return std::log10(a); }
template <typename T> T sqrt_int(T a) {
T x = std::sqrt(a);
while (x * x > a) {
--x;
}
while ((x + 1) * (x + 1) < a) {
++x;
}
return x;
}
/**************/
/* random.hpp */
/**************/
uint32_t xorshift() {
static uint32_t y = 2463534242;
y = y ^ (y << 13);
y = y ^ (y >> 17);
return y = y ^ (y << 15);
}
/***************************/
/* simulated_annealing.hpp */
/***************************/
class SimulatedAnnealing {
double temp, alpha;
public:
SimulatedAnnealing(double init_temp, double alpha)
: temp(init_temp), alpha(alpha) {}
double operator()() {
double threshold = temp * log(xorshift() / double(1ll << 32));
temp *= alpha;
return threshold;
}
};
/**************/
/* string.hpp */
/**************/
#include <sstream>
#include <string>
class String : public std::string {
public:
constexpr static auto npos = std::string::npos;
String() : std::string() {}
String(char c) : std::string(1, c) {}
String(int n) : std::string(std::to_string(n)) {}
String(long n) : std::string(std::to_string(n)) {}
String(long long n) : std::string(std::to_string(n)) {}
String(const char *s) : std::string(s) {}
String(const std::string &s) : std::string(s) {}
String(int n, char c) : std::string(n, c) {}
String(Input &in) {
std::cin >> *this;
in.eof = std::cin.eof();
}
String &operator+=(const String &s) { return *this = *this + s; }
String operator+(const String &s) const { return std::operator+(*this, s); }
String &operator+=(const char *s) { return *this = *this + s; }
String operator+(const char *s) const { return std::operator+(*this, s); }
String &operator+=(char s) { return *this = *this + s; }
String operator+(char s) const { return std::operator+(*this, s); }
String operator*(int n) const {
String res;
for (int i = 0; i < n; ++i) {
res += *this;
}
return res;
}
static String getline() {
String v;
std::getline(std::cin, v);
in.eof = std::cin.eof();
return v;
}
String substr(size_t pos, size_t n_pos = std::string::npos) {
return std::string::substr(pos, n_pos);
}
String &reverse() {
std::reverse(this->begin(), this->end());
return *this;
}
Vector<String> split(char delim = ' ') const {
std::stringstream ss(*this);
Vector<String> res;
for (std::string tmp; std::getline(ss, tmp, delim);) {
if (tmp != "") {
res.emplace_back(tmp);
}
}
return res;
}
String &toupper() {
for (auto &c : *this) {
c = std::toupper(c);
}
return *this;
}
String &tolower() {
for (auto &c : *this) {
c = std::tolower(c);
}
return *this;
}
template <typename Function> String transform(Function func) const {
String res;
std::transform(this->begin(), this->end(), std::back_inserter(res), func);
return res;
}
bool contains(const String &s) const {
return this->find(s) != std::string::npos;
}
String &erase(char c) {
String res;
for (char i : *this) {
if (i != c) {
res += i;
}
}
return *this = res;
}
int count(char c) const { return std::count(this->begin(), this->end(), c); }
template <bool Repeat = false>
String &replaceAll(const String &from, const String &to) {
for (size_t pos = 0; (pos = this->find(from, pos)) != std::string::npos;) {
this->replace(pos, from.size(), to);
if (!Repeat) {
pos += to.size();
}
}
return *this;
}
String &sort() {
std::sort(this->begin(), this->end());
return *this;
}
String &rsort() {
std::sort(this->rbegin(), this->rend());
return *this;
}
String &unique() {
std::string::erase(std::unique(this->begin(), this->end()), this->end());
return *this;
}
String &rotate(int n) {
std::rotate(this->begin(), this->begin() + n, this->end());
return *this;
}
bool next_permutation() {
return std::next_permutation(this->begin(), this->end());
}
template <typename T, typename Function1, typename Function2>
T inner_product(const String &v, T init, Function1 func1,
Function2 func2) const {
return std::inner_product(this->begin(), this->end(), v.begin(), init,
func1, func2);
}
template <typename Function> int find_if(Function func) const {
return std::find_if(this->begin(), this->end(), func) - this->begin();
}
template <typename Function> bool all_of(Function func) const {
return std::all_of(this->begin(), this->end(), func);
}
template <typename Function> bool any_of(Function func) const {
return std::any_of(this->begin(), this->end(), func);
}
template <typename Function> bool none_of(Function func) const {
return std::none_of(this->begin(), this->end(), func);
}
Vector<int> find_all(const String &s) const {
Vector<int> res;
auto pos = this->find(s);
while (pos != String::npos) {
res.emplace_back(pos);
pos = this->find(s, pos + 1);
}
return res;
}
int size() const { return std::string::size(); }
operator int() const { return std::stoi(*this); }
operator long() const { return std::stol(*this); }
operator long long() const { return std::stoll(*this); }
operator float() const { return std::stof(*this); }
operator double() const { return std::stod(*this); }
operator long double() const { return std::stold(*this); }
};
bool islower(char c) { return 'a' <= c && c <= 'z'; }
bool isupper(char c) { return 'a' <= c && c <= 'z'; }
/*************/
/* timer.hpp */
/*************/
#include <chrono>
struct Timer {
std::chrono::time_point<std::chrono::system_clock> start;
Timer() : start(std::chrono::system_clock::now()) {}
double seconds() {
auto now = std::chrono::system_clock::now();
return std::chrono::duration<double>(now - start).count();
}
};
/************/
/* main.cpp */
/************/
#include <bitset>
constexpr int dp[4] = {-27, -1, 27, 1};
Vector<short> t[500], cnt_p(729);
Vector<std::bitset<729>> bit(500);
short rcnt[729], f[500], ar[500][729], nxt[32][729][4];
char nxt_d[729][16];
void update_nxt_d(int p, int a) {
for (int i = 0; i < 4; ++i) {
for (int j = 0; j < 4; ++j) {
if (a == 0) {
nxt_d[p][4 * i + j] = i & 3;
} else {
nxt_d[p][4 * i + j] = a * j & 3;
}
}
}
}
int simulate_init() {
int score = 5000;
for (int i = 0; i < 500; ++i) {
int p = 364, d = 0;
for (int j = 0; j < t[i].size(); ++j) {
auto c = t[i][j];
d = (nxt_d[p][c & 15] + d) & 3;
p = nxt[c / 16][p][d];
if (!bit[i][p]) {
++rcnt[p];
bit[i][p] = 1;
ar[i][p] = (j + 1) * 4 + d;
}
}
++cnt_p[p];
if (cnt_p[p] >= 2) {
if (cnt_p[p] == 2) {
score -= 17;
} else if (cnt_p[p] == 3) {
score -= 12;
} else if (cnt_p[p] == 4) {
score -= 11;
} else {
score -= 10;
}
}
f[i] = p;
}
return score;
}
int simulate(int th, int cp) {
auto cnt = cnt_p;
int diff = 0;
Vector<int> v;
v.reserve(64);
for (int i = 0; i < 500; ++i) {
if (!bit[i][cp]) {
continue;
}
v.emplace_back(i);
int p = f[i];
if (cnt[p] >= 2) {
if (cnt[p] == 2) {
diff += 17;
}
if (cnt[p] == 3) {
diff += 12;
}
if (cnt[p] == 4) {
diff += 11;
}
if (cnt[p] >= 5) {
diff += 10;
}
}
--cnt[p];
}
for (int i : v) {
int p = cp, d = ar[i][p] & 3;
for (int j = ar[i][p] / 4; j < t[i].size(); ++j) {
auto c = t[i][j];
d = (nxt_d[p][c & 15] + d) & 3;
p = nxt[c / 16][p][d];
}
++cnt[p];
if (cnt[p] >= 2) {
if (cnt[p] == 2) {
diff -= 17;
} else if (cnt[p] == 3) {
diff -= 12;
} else if (cnt[p] == 4) {
diff -= 11;
} else {
diff -= 10;
}
if (diff < th) {
return diff;
}
}
}
return diff;
}
void reset(int cp) {
for (int i = 0; i < 500; ++i) {
if (!bit[i][cp]) {
continue;
}
int p = cp, d = ar[i][p] & 3;
for (int j = ar[i][p] / 4; j < t[i].size(); ++j) {
auto c = t[i][j];
d = (nxt_d[p][c & 15] + d) & 3;
p = nxt[c / 16][p][d];
if (ar[i][p] / 4 == j + 1) {
--rcnt[p];
bit[i][p] = 0;
ar[i][p] = 0;
}
}
--cnt_p[f[i]];
}
}
void update(int cp) {
for (int i = 0; i < 500; ++i) {
if (!bit[i][cp]) {
continue;
}
int p = cp, d = ar[i][p] & 3;
for (int j = ar[i][p] / 4; j < t[i].size(); ++j) {
auto c = t[i][j];
d = (nxt_d[p][c & 15] + d) & 3;
p = nxt[c / 16][p][d];
if (!bit[i][p]) {
++rcnt[p];
bit[i][p] = 1;
ar[i][p] = (j + 1) * 4 + d;
}
}
f[i] = p;
++cnt_p[p];
}
}
int main() {
Timer timer;
double init_temp = 6.5;
double alpha = 1 - 6.5e-7;
double threthold = 40;
int n(in), m(in), l(in);
(void)m;
(void)l;
Vector<String> s(n, in);
for (int i = 0; i < 500; ++i) {
t[i].emplace_back(0);
for (int j = 0; j < 300; ++j) {
if (s[i][j] == 'S') {
t[i].back() += 16;
} else {
if (t[i].back() / 16) {
t[i].emplace_back(0);
}
if (t[i].back() % 4 == 3) {
t[i].back() -= 4;
}
int r = s[i][j] == 'L' ? 1 : 3;
t[i].back() += 4 * r + 1;
t[i].back() %= 16;
}
}
}
Vector<int> a(729, 1);
for (int i = 0; i < 27; ++i) {
a[i * 27 + 13] = 2;
}
for (int i = 5; i <= 21; ++i) {
a[i] = 2;
}
for (int i = 5; i <= 21; ++i) {
a[i + 27 * 26] = 2;
}
a[13] = a[13 + 27 * 26] = 0;
a[0] = a[26] = a[26 * 27] = a[27 * 27 - 1] = 2;
a[1] = a[25] = a[26 * 27 + 1] = a[27 * 27 - 2] = 2;
a[27] = a[26 + 27] = a[25 * 27] = a[26 * 27 - 1] = 2;
for (int i = 0; i < 729; ++i) {
for (int j = 0; j < 4; ++j) {
nxt[0][i][j] = i;
}
}
for (int k = 1; k < 32; ++k) {
for (int i = 0; i < 729; ++i) {
if (a[i] != 2) {
for (int j = 0; j < 4; ++j) {
int d = dp[j];
if (j == 0 && i < 27) {
d = 0;
}
if (j == 1 && i % 27 == 0) {
d = 0;
}
if (j == 2 && i > 701) {
d = 0;
}
if (j == 3 && i % 27 == 26) {
d = 0;
}
nxt[k][i][j] = nxt[k - 1][i + d][j];
}
} else {
for (int j = 0; j < 4; ++j) {
int ii = i;
int d = dp[j];
if (j == 0 && i < 27) {
d = 0;
}
if (j == 1 && i % 27 == 0) {
d = 0;
}
if (j == 2 && i > 701) {
d = 0;
}
if (j == 3 && i % 27 == 26) {
d = 0;
}
ii += d;
d = dp[j];
if (j == 0 && ii < 27) {
d = 0;
}
if (j == 1 && ii % 27 == 0) {
d = 0;
}
if (j == 2 && ii > 701) {
d = 0;
}
if (j == 3 && ii % 27 == 26) {
d = 0;
}
ii += d;
nxt[k][i][j] = nxt[k - 1][ii][j];
}
}
}
}
for (int p = 0; p < 729; ++p) {
update_nxt_d(p, a[p]);
}
int score = simulate_init();
int best_score = score;
auto best_a = a;
SimulatedAnnealing sa(init_temp, alpha);
// for (int i = 0; i < 1300000; ++i) {
for (int i = 0;; ++i) {
if (i % 1024 == 0) {
if (timer.seconds() > 2.9) {
break;
}
}
int p = -1, c;
while (true) {
p = xorshift() % 729;
if (rcnt[p] > threthold || a[p] == 2 || p == 13 || p == 13 + 27 * 26) {
continue;
}
c = xorshift() % 4;
if (c != 2 && c != a[p]) {
break;
}
}
update_nxt_d(p, c);
int th = ceil(sa());
int diff = simulate(th, p);
if (diff >= th) {
update_nxt_d(p, a[p]);
reset(p);
update_nxt_d(p, c);
update(p);
a[p] = c;
score = score + diff;
if (best_score < score) {
best_score = score;
best_a = a;
}
} else {
update_nxt_d(p, a[p]);
}
}
cout << String(29, '#') << endl;
for (int i = 0; i < 27; ++i) {
cout << '#';
for (int j = 0; j < 27; ++j) {
cout << ".LDR"[best_a[27 * i + j]];
}
cout << '#' << endl;
}
cout << String(29, '#') << endl;
}
提出情報
| 提出日時 |
|
| 問題 |
A - ばらばらロボット |
| ユーザ |
not |
| 言語 |
C++ (GCC 9.2.1) |
| 得点 |
142086 |
| コード長 |
33498 Byte |
| 結果 |
AC |
| 実行時間 |
2910 ms |
| メモリ |
5440 KiB |
ジャッジ結果
| セット名 |
Sample |
All |
| 得点 / 配点 |
4743 / 5000 |
137343 / 145000 |
| 結果 |
|
|
| セット名 |
テストケース |
| Sample |
example_01.txt |
| All |
subtask_01_02.txt, subtask_01_03.txt, subtask_01_04.txt, subtask_01_05.txt, subtask_01_06.txt, subtask_01_07.txt, subtask_01_08.txt, subtask_01_09.txt, subtask_01_10.txt, subtask_01_11.txt, subtask_01_12.txt, subtask_01_13.txt, subtask_01_14.txt, subtask_01_15.txt, subtask_01_16.txt, subtask_01_17.txt, subtask_01_18.txt, subtask_01_19.txt, subtask_01_20.txt, subtask_01_21.txt, subtask_01_22.txt, subtask_01_23.txt, subtask_01_24.txt, subtask_01_25.txt, subtask_01_26.txt, subtask_01_27.txt, subtask_01_28.txt, subtask_01_29.txt, subtask_01_30.txt |
| ケース名 |
結果 |
実行時間 |
メモリ |
| example_01.txt |
AC |
2906 ms |
5308 KiB |
| subtask_01_02.txt |
AC |
2903 ms |
5440 KiB |
| subtask_01_03.txt |
AC |
2904 ms |
5304 KiB |
| subtask_01_04.txt |
AC |
2903 ms |
5276 KiB |
| subtask_01_05.txt |
AC |
2907 ms |
5376 KiB |
| subtask_01_06.txt |
AC |
2904 ms |
5200 KiB |
| subtask_01_07.txt |
AC |
2910 ms |
5276 KiB |
| subtask_01_08.txt |
AC |
2908 ms |
5272 KiB |
| subtask_01_09.txt |
AC |
2907 ms |
5252 KiB |
| subtask_01_10.txt |
AC |
2903 ms |
5376 KiB |
| subtask_01_11.txt |
AC |
2903 ms |
5392 KiB |
| subtask_01_12.txt |
AC |
2907 ms |
5352 KiB |
| subtask_01_13.txt |
AC |
2907 ms |
5376 KiB |
| subtask_01_14.txt |
AC |
2907 ms |
5196 KiB |
| subtask_01_15.txt |
AC |
2904 ms |
5372 KiB |
| subtask_01_16.txt |
AC |
2909 ms |
5200 KiB |
| subtask_01_17.txt |
AC |
2903 ms |
5276 KiB |
| subtask_01_18.txt |
AC |
2904 ms |
5304 KiB |
| subtask_01_19.txt |
AC |
2905 ms |
5264 KiB |
| subtask_01_20.txt |
AC |
2903 ms |
5376 KiB |
| subtask_01_21.txt |
AC |
2908 ms |
5392 KiB |
| subtask_01_22.txt |
AC |
2904 ms |
5308 KiB |
| subtask_01_23.txt |
AC |
2904 ms |
5316 KiB |
| subtask_01_24.txt |
AC |
2907 ms |
5376 KiB |
| subtask_01_25.txt |
AC |
2903 ms |
5200 KiB |
| subtask_01_26.txt |
AC |
2903 ms |
5196 KiB |
| subtask_01_27.txt |
AC |
2909 ms |
5256 KiB |
| subtask_01_28.txt |
AC |
2906 ms |
5256 KiB |
| subtask_01_29.txt |
AC |
2904 ms |
5440 KiB |
| subtask_01_30.txt |
AC |
2903 ms |
5324 KiB |